php - PHP 中的 session 注销问题

标签 php mysql html

我正在尝试为页面制作登录和注销脚本,但由于某种原因它对我来说效果不佳。在我尝试注销之前,它似乎工作正常。它似乎破坏了 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/

相关文章:

php - 使用 imagejpeg() 函数保存图像文件

php - MySQL 中的 Unicode 字符在 PHP 和 ASP 中返回不同的字符代码值

javascript - 使用 React 和 Web 组件

mysql - NodeJS、Redis 和 MySQL

javascript - 如何使用元素的实时宽度同时调整另一个div的大小?

html - 如何使div的高度等于宽度

php - 使用单个查询和每个用户密码 salt 的用户登录

php - 向 PHPExcel 对象插入复选标记

php - 增加 MySQL 查询的深度

mysql - 在mysql中对与字母数值混合的数值进行排序