我正在尝试为页面制作登录和注销脚本,但由于某种原因它对我来说效果不佳。在我尝试注销之前,它似乎工作正常。它似乎破坏了 session 变量,但它仍然让我查看页面。 这是我的登录代码:
代码: 登录.php
<?php
// Use session variable on this page. This function must put on the top of page.
session_start();
////// Logout Section. Delete all session variable.
session_destroy();
$Name=$_POST['Name'];
$Pass=$_POST['Pass'];
// To protect MySQL injection (more detail about MySQL injection)
$Name = stripslashes($Name);
$Pass = stripslashes($Pass);
$Name = mysql_real_escape_string($Name);
$Pass = mysql_real_escape_string($Pass);
$sql="SELECT * FROM reg1 WHERE uname='$Name' and pass='$Pass'";
$result=mysql_query($sql);
if(mysql_num_rows($result)!='0') // If match.
{
session_register("uname"); // Craete session username.
header("location:loged.php"); // Re-direct to loged.php
exit;
}else{ // If not match.
echo '<script type="text/javascript">
window.alert("Wrong UserName And Password");
window.location="index.php"
</script>';
}
// End Login authorize check.
?>
注销.php
<小时/><?php
// Inialize session
session_start();
// Delete certain session
unset($_SESSION['uname']);
// Delete all session variables
session_destroy();
// Jump to login page
header("Location: index.php?msg=Successfully Logged out");
}
?>
谢谢大家...
最佳答案
您正在设置 session ,但您没有在任何地方检查它是否已设置。意味着您没有检查用户是否已登录..您需要这样做
if (!isset($_SESSION['uname'])) /*If uname not set then it is a guest*/
{
//page contents for guest user
}
else
{
//page for authenticated user.
}
关于php - PHP 中的 session 注销问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6014088/