会议??如何显示用户行? [英] Sessions?? How can I display a the users row?

查看:44
本文介绍了会议??如何显示用户行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

  • 使用会话(将用户 ID 保存在会话中,并使用类似 where id = {$id}
  • 之类的内容将其添加到查询中
  • 从您的登录代码中获取您的用户 ID.因此,检查用户是否登录的相同代码可以返回用户 ID.

您当前的代码显示了您的登录方式,这有效吗?然后您应该可以在您之前编写的代码中使用您的会话.

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

这篇关于会议??如何显示用户行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆