简体   繁体   中英

Sessions?? How can I display a the users row?

I want to display the attributes of the game character, which is under the users TABLE. So, I want it to display the specific attributes of the user who has logged in, since it should be in his row. Do I need to register my users with session, because I didn't.

This is the code I used to get the sessions for the user in when login in

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


            }
            }
    }
}
?>

you need to find which user you are logged in as. How do you log in to your system? You have several options which you can try out:

  • use sessions (save the userID in the session, and add that to the query using something like where id = {$id}
  • Get your userid from your log-in code. So the same code that checks if a user is logged in, can return a userid.

Your current code shows how you log In, and this works? Then you should be able to use your session in the code you had up before.

Just as an example, you need to check this, and understand the other code. It feels A bit like you don't really understand the code you've posted, so it's hard to show everything, but it should be something like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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