我的登录脚本出了问题。 我有一个小div,我想回复一个表单,以便用户可以登录。在提交表单后,这些值将被发送到另一个创建会话的页面。之后,当用户返回登录表单所在的索引时,不再有登录表单,而是有关用户的信息。但问题是:即使没有创建会话,div中也没有回应。
<div id="updates">
<?php
if(isset($_SESSION['username']))
{
$query = mysql_query("SELECT * FROM users WHERE username='$dbusername'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
$dbvipshouts = $row['vip_shouts'];
echo"
<table>
<tr>
<td><font color='white'>Nickname:</font></td><td>$dbusername</td>
</tr>
<tr>
<td><font color='white'>Vip shouts over:</font></td><td>$dbvipshouts</td>
</tr>
</table>";
}
}
else
{
echo "
<form name='login' action='login.php' method='POST'>
<table>
<tr>
<td><font color='white'>Nickname:</font></td><td><input class='inputname' type='text' name='nickname'></td>
</tr>
<tr>
<td><font color='white'>Wachtwoord:</font></td><td><input class='inputname' type='password' name='password'></td>
</tr>
<tr>
<td colspan='2'><center><input class='inputname' type='submit' name='submit' value='Log in!'></center></td>
</tr>
</table>
</form>";
}
}
?>
</div>
以及应该创建会话的文件的代码:
<?php
$username = $_POST["nickname"];
$password = $_POST["password"];
if ($username&&$password)
{
$query = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row = mysql_fetch_assoc($query))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
$dbvipshouts = $row['vip_shouts'];
}
// controleren of ze bij elkaar passen
if ($username==$dbusername&&$password==$dbpassword)
{
$_SESSION['username']==$dbusername;
$_SESSION['vip_shouts']==$dbvipshouts;
header('Location:index.php');
}
else
echo "Gebruikersnaam of wachtwoord is onjuist! Klik <a href='index.php'>hier</a> om terug te gaan.";
}
else
die("Deze gebruiker bestaat niet! Klik <a href='index.php'>hier</a> om terug te gaan.");
}
else
die ("Voer een gebruikersnaam en wachtwoord in! Klik <a href='index.php'>hier</a> om terug te gaan.");
?>
答案 0 :(得分:1)
首先,这个:
$_SESSION['username']==$dbusername;
$_SESSION['vip_shouts']==$dbvipshouts;
应该是这样的:
$_SESSION['username']=$dbusername;
$_SESSION['vip_shouts']=$dbvipshouts;
并确保您正在使用session_start();
答案 1 :(得分:0)
您必须在页面开始时使用session_start(),无论您想在何处创建或使用会话数据
例如
<?php
session_start();
$_SESSION['id']=1;
?>
在其他页面
<?php
session_start();
echo $_SESSION['id'];
?>