PHP:记录用户

时间:2014-09-22 08:10:31

标签: php html mysql logout

我有一个允许用户输入数据的表单。然后,它会根据数据库检查这些数据,以查看用户是否存在。如果是这样,它会将它们记录到某个页面中。

然后我想允许他们注销(这样他们就无法访问该特定页面)。为此,我创建了一个" logout.php"我试图清除登录详细信息的文档。

但是,完成此操作后,如果我尝试加载登录页面,则会将我带回登录页面。

这是我的代码(login.php - 创建表单并记录用户):

<?php  //Start the Session
session_start();

require('connect.php');
if (isset($_POST['username']) and isset($_POST['password']))
{
    //3.1.1 Assigning posted values to variables.
    $username = $_POST['username'];
    $password = $_POST['password'];

    //3.1.2 Checking if the values exist in the database
    $checkLogin = $connection->query("SELECT * FROM users 
        where (username='$username' && password='$password')");
    $numRows = $checkLogin->fetchColumn();
    //3.1.2 If the posted values are equal to the database values, then session will be created for the user.
    if ($numRows >= 1){
        $_SESSION['username'] = $username;
    }else{
        //3.1.3 If the login credentials doesn't match, he will be shown with an error message.
        echo '<script>window.alert("Invalid Login Credentials")</script>';
    }
}
//3.1.4 if the user is logged in Greets the user with message
if (isset($_SESSION['username'])){
$username = $_SESSION['username'];
echo "Hi " . $username . "
";
echo "This is the Members Area";
echo "<a href='logout.php'>Logout</a>";
echo $username;

}else{
//3.2 When the user visits the page first time, simple login form will be displayed.
?>
<!DOCTYPE html>
 <head>
<title>CodingCyber - Simple Login Script</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<!-- Form for logging in the users -->

<div class="register-form">
<?php
    if(isset($msg) & !empty($msg)){
        echo $msg;
    }
 ?>
<h1>Login</h1>
<form action="login.php" method="POST">
    <p><label>User Name : </label>
    <input id="username" type="text" name="username" placeholder="username" /></p>

     <p><label>Password&nbsp;&nbsp; : </label>
     <input id="password" type="password" name="password" placeholder="password" /></p>

    <a class="btn" href="register.php">Signup</a>
    <input class="btn register" type="submit" name="submit" value="Login" />
    </form>
</div>
<?php } ?>
</body>
</html>

&#34;要求(&#39; connect.php&#39;)&#34 ;;只是连接到我的MySQL数据库。这段代码似乎都运行正常,因为它确实会记录用户。我只是为了完整性而把它包括在内。问题。

正如您所看到的,登录后会显示文字,说明&#34;会员区&#34;,并带有退出超链接。

这是我的logout.php代码(我希望删除对该成员区域的访问权限,并将用户带回登录页面):

<?php
    session_start();
    $username = '';
    $password = '';
    $confirmPassword = '';
    $email = '';
    echo $username;
    unset($_POST['username']);
    unset($password);

?>

第二位代码就是说实话,我真的不确定我要删除访问权限的意图。

我已经查看了其他几个问题,但似乎无法找到解决方案。

任何帮助都会很棒!如果有类似的帖子或者您需要更多信息,请告诉我。

谢谢!

2 个答案:

答案 0 :(得分:5)

试试这个:

unset($_SESSION['username']);

它将从会话中删除用户名变量

答案 1 :(得分:3)

您需要销毁会话变量:

    // Unset all of the session variables.
    $_SESSION = array();

    // If it's desired to kill the session, also delete the session cookie.
    // Note: This will destroy the session, and not just the session data!
    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000,
            $params["path"], $params["domain"],
            $params["secure"], $params["httponly"]
        );
    }
    // Finally, destroy the session.
    session_destroy();
    $url = 'http://example.com';
    header( "Location: $url" );
    exit();