gpt4 book ai didi

php - session ??如何显示用户行?

转载 作者:行者123 更新时间:2023-11-29 00:57:38 24 4
gpt4 key购买 nike

我想显示游戏角色的属性,在users表下。所以,我希望它显示已登录用户的特定属性,因为它应该在他的行中。我是否需要为我的用户注册 session ,因为我没有。

这是我用来在登录时为用户获取 session 的代码

<?
if(isset($_POST['Login'])) {

if (ereg('[^A-Za-z0-9]', $_POST['name'])) {// before we fetch anything from the database we want to see if the user name is in the correct format.
echo "Invalid Username.";
}else{

$query = "SELECT password,id,login_ip FROM users WHERE name='".mysql_real_escape_string($_POST['Username'])."'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result); // Search the database and get the password, id, and login ip that belongs to the name in the username field.

if(empty($row['id'])){
// check if the id exist and it isn't blank.
echo "Account doesn't exist.";
}else{

if(md5($_POST['password']) != $row['password']){
// if the account does exist this is matching the password with the password typed in the password field. notice to read the md5 hash we need to use the md5 function.
echo "Your password is incorrect.";
}else{

if(empty($row['login_ip'])){ // checks to see if the login ip has an ip already
$row['login_ip'] = $_SERVER['REMOTE_ADDR'];
}else{

$ip_information = explode("-", $row['login_ip']); // if the ip is different from the ip that is on the database it will store it

if (in_array($_SERVER['REMOTE_ADDR'], $ip_information)) {
$row['login_ip'] = $row['login_ip'];
}else{
$row['login_ip'] = $row['login_ip']."-".$_SERVER['REMOTE_ADDR'];
}
}

$_SESSION['user_id'] = $row['id'];// this line of code is very important. This saves the user id in the php session so we can use it in the game to display information to the user.

$result = mysql_query("UPDATE users SET userip='".mysql_real_escape_string($_SERVER['REMOTE_ADDR'])."',login_ip='".mysql_real_escape_string($row['login_ip'])."' WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'")
or die(mysql_error());

// to test that the session saves well we are using the sessions id update the database with the ip information we have received.

header("Location: play.php"); // this header redirects me to the Sample.php i made earlier


}
}
}
}
?>

最佳答案

您需要找到您登录的用户身份。你如何登录到你的系统?您有多种选择可以尝试:

  • 使用 session (将用户 ID 保存在 session 中,然后使用类似 where id = {$id}
  • 的方式将其添加到查询中
  • 从您的登录代码中获取您的用户 ID。因此,检查用户是否登录的相同代码可以返回用户 ID。

您当前的代码显示了您如何登录,这行得通吗?然后您应该能够在之前的代码中使用您的 session 。

举个例子,你需要检查这个,并理解其他代码。感觉有点像你没有真正理解你发布的代码,所以很难显示所有内容,但它应该是这样的。

<?php 
session_start();
$id = $_SESSION['user_id'];
//you need to do some checking of this ID! sanitize here!
$result = mysql_query("SELECT * FROM users" where id = {$id}) or die(mysql_error());
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
}

关于php - session ??如何显示用户行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5211694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com