如何使用PHP制作涉及会话cookie的注销页面?

时间:2014-04-07 12:35:11

标签: php html mysql validation logout

我想创建一个logout.php,以便记住login.php中的用户名。

我想使用$ _GET方法,以便从登录中获取会话用户名,当我们注销时,cookie会话将被注销。如果没有设置cookie会话,则会出现错误,说没有登录,因此日志痛风不起作用。

login.php登录用户并设置我从浏览器设置检查的cookie。

login.php

<?php 

require"connection.php";
if (!isset($_POST['submit'])){
    $user = $_POST['username']; 
    $password = $_POST['password'];  

  if ($user){ 
        if ($password){ 

setcookie('username', '$user', time()+3600); 
setcookie('password', '$password', time()+3600);       

          //make sure login info correct
            $query = mysql_query("SELECT * FROM users WHERE username = '$user'"); 
                $numrows = mysql_num_rows($query); 

            if ($numrows == 1) {
                $row = mysql_fetch_assoc($query); 
                 $dbuser = $row['username']; 
                 $dbpass = $row['password']; 

        echo '<script type="text/javascript">alert("Welcome, '.$user.'. A cookie session  has been created.");</script>'; 
        }else{echo "Please enter valid username or password";}
        }else {echo "Your password didn't match. Please try again";}
        }else {echo "Your username didn't match. Please try again";}
        }else {echo "Please enter username and password";}
?> 

1 个答案:

答案 0 :(得分:1)

尝试这样的事情..

if(isset($_COOKIE['username']) && !empty($_COOKIE['username'])) {
    // Bye $_COOKIE['username'], you are logged out.
    setcookie('username', '', time() - 3600);
} else {
    // no user logged in
}