注销不会正确破坏会话

时间:2013-12-01 16:56:45

标签: php session session-variables logout

我是PHP的新手,在我第一次尝试将用户注销时,它无效。 的login.php:

<?php include_once("config.php"); ?>
<?php
$login = @$_POST['login'];
$un = strip_tags(@$_POST['uname']);
$un = mysql_real_escape_string($un);

$pass = strip_tags(@$_POST['pass']);
$pass = mysql_real_escape_string($pass);
$pass = md5($pass);

if ($login) {
if (null!==($un && $pass)) {
$sql = mysql_query("SELECT * FROM users WHERE username='$un' AND password='$pass'");
$num_rows = mysql_num_rows($sql);
if ($num_rows == 1) {
    session_start();
    $_SESSION['username'] = $un;
    header("location: home.php");
} else {
    echo "The username or password is incorrect, please try again";
}
} else {
echo "Please fill out all the fields";
}
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div id="login_wrapper">
        <h2>Thank you for signing up</h2>
        <h4>Please login to your account below</h4>
        <form action="login.php" method="POST">
            <input type="text" name="uname" placeholder="Username"><br>
            <input type="password" name="pass" placeholder="Password"><br>
            <input type="submit" name="login" value="Login">
        </form>
    </div>
</body>
</html>

此脚本正常运行,并将其重定向到主页。下面是home.php的脚本:

<?php include_once("config.php"); ?>
<?php
session_start();
if (empty($_SESSION['username'])) {
   echo "Please login <a href='login.php'>here</a>";
} else {
   echo "Welcome ". $_SESSION["username"]. "<br>";
   echo "Logout <a href='index.php'>here</>";
}
?>

这会正确显示此人的用户名。下面是logout.php代码:

<?php
session_start();
$_session = array();
session_destroy();
die(header("location:index.php"));
?>

我被重定向到index.php页面,但如果我去home.php,它仍会显示我的用户名。我做错了什么?

ps:对于PHP来说,我是新手,所以请放轻松我:)

0 个答案:

没有答案