如何在会话中显示用户ID?

时间:2013-08-10 14:24:47

标签: php mysql session sessionid

除了页面中的用户名和用户ID之外,我想获得。关于我,我创建了两个PHP页面。我的数据库还包括3列userid,username,password。 login.php页面是

<?php 
session_start();
//@$userid = $_GET['userid'];
@$username = $_POST['username'];
@$password = $_POST['pass'];

if(@$_POST['Submit']){
if($username&&$password)
{
$connect = mysql_connect("localhost","*****","") or die("Cannot Connect");
mysql_select_db("project") or die("Cannot find the database");

$query = mysql_query("SELECT * FROM users WHERE username='$username'");
//$query = mysql_query("SELECT * FROM users WHERE userid='$userid' and username='$username'");
$numrows = mysql_num_rows($query);
if($numrows!=0)
{
    while ($row = mysql_fetch_assoc($query))
    //while ($row = mysql_fetch_array($query))
    {
        $dbuserid = $row['userid'];
        $dbusername = $row['username'];
        $dbpassword = $row['password'];
    }
    if($username==$dbusername&&$password==$dbpassword)
    {
        echo "You are login!!!!! Continue now with the survey <a href='mainpage.php'>here</a>";
        $_SESSION['username']=$username;
        $_SESSION['userid']=$userid;
    }
    else
    {
        echo "<b>Incorrect Password!!!!</b>";
    }
}
else
    //die("That user does not exist");
    echo "<b>That user does not exist</b>";
}
else
echo "<b>You must enter a username and a password</b>";
}
?>
<!--<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />-->
<title>Login Page</title>
<style type="text/css"> 
h2 {letter-spacing: 10px; font-size: .2in; background-color: #33CC00; color: #000000; text-transform:uppercase; width:260px}
span {color: #FF00CC}
legend {font-variant: small-caps; font-weight: bold}
fieldset {width: 260px; height: 100px; font-family: "Times New Roman", Times, serif; background-color: #CCCCCC; color: #000000}
label {display:block;}
.placeButtons {position: relative; left: 0px; width: 70px; margin: 5px; 0px;}
</style>
</head>

<body background="images/good.jpg">

<h2>Login Page</h2>
<form name="loginform" method='POST'>

<fieldset>
<legend>Form</legend>
    <label>Username: <input type="text" name="username"/><span>*</span></label><br/>
    <label>Password: <input type="password" name="pass"/><span>*</span></label>
    <input class="placeButtons" type="reset" value='Reset'/>
    <input class="placeButtons" type="submit" name="Submit" value='Login'/>
    <a href='registration.php'>Register</a>
</fieldset><br>
<a href='firstpage.php'><-- Go Back</a>
</form>
</body>
</html>

和页面是用户的欢迎页面

<?php 
session_start();

if ($_SESSION['username'])
{
//echo "Welcome, ".$_SESSION['username']."! <a href='logout.php'>Logout</a>";
echo "Welcome, ".$_SESSION['username']."<br>".$_SESSION['userid']. "<a href='logout.php'>Logout</a>";

}
else
die("You must be logged in!!");
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />-->
<title></title>

</head>
<body background="images/good.jpg">
</body>
</html>

问题是在欢迎页面中它只显示用户名而不是UserID。我错过了什么?此外,我知道我的登录页面不是最好的,并且是SQL注入攻击的典型示例。我必须改进它。

2 个答案:

答案 0 :(得分:0)

我注意到了一个快速的事情。这可能是问题所在。 $_SESSION['userid']$userid获取未设置的值。同样使用@来压制你的错误也不是一个好习惯。使用isset检查变量是否已设置并继续。

$_SESSION['userid'] = $userid; //where are you getting $userid from?

这应该是

 $_SESSION['userid'] = $dbuserid;

而不是使用像

这样的语句
if ($_SESSION['username'])

首先检查变量是否设置为

if ( isset($_SESSION['username']) ){
 //now continue your work
}

答案 1 :(得分:0)

并确保在共享的webhost上使用ini_set('session_save_path','new_dir')或函数session_save_path。来自不同网站的同一目录中的会话易于进行会话窃取/窥探/修改。

我检查了PHP源代码PHP没有跟踪哪个会话ID是由网站(HOST)制作的,如果攻击者在同一个虚拟主机上拥有一个帐户,这个攻击的原因是什么

所以永远不要太信任SESSION数组,因为你认为它是安全的,因为它是由服务器生成的 如果你不采取反措施就不行......