繁体   English   中英

会议? 如何显示用户行?

[英]Sessions?? How can I display a the users row?

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

这是我登录时用于获取用户会话的代码

<?
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


            }
            }
    }
}
?>

您需要找到您以哪个用户身份登录。 您如何登录到系统? 您可以尝试以下几种方法:

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

您当前的代码显示您如何登录,这有效吗? 然后,您应该能够在之前的代码中使用会话。

仅作为示例,您需要检查一下并了解其他代码。 感觉有点像您不太了解所发布的代码,因此很难显示所有内容,但是应该是这样的。

<?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 )) {
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM