PHP注销脚本

时间:2012-07-23 05:59:42

标签: php

我正在尝试创建一个PHP注销脚本,但是,只要我实现它,页面就会在登录后重定向到页面。这是代码:

Connect.php

<?php 
    session_start();
    $link = mysql_connect("localhost", "database_name", "database_pass") or die(mysql_error());
    mysql_select_db("database_name") or die(mysql_error());

    if(isset($_COOKIE['username'])) 
             $_SESSION['username'] = $_COOKIE['username'];
 ?>  

Logout.php

<?php
    include("connect.php");
    session_start();
    session_destroy();
    $username = $_SESSION['username'];
setcookie($username, time()-3600);  
    header("Location: index.php");
    die;    
?> //immediately after here, instead of going to index.php(the login page), it goes straight to the page that would appear after if the user had logged in(control_panel.php). 

有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:2)

查看此页面:http://blog.ircmaxell.com/2011/08/security-review-creating-secure-php.html

完全通过“创建安全的PHP登录脚本”。您当前的解决方案存在许多安全问题。

答案 1 :(得分:1)

你的代码错了我猜,问题出在这里..

在您的logout.php中,在您取消会话后,您的cookie将过期,您的cookie不会过期。

在connect.php中,您正在使用此条件再次设置会话并设置因为cookie仍在用户的浏览器中

if(isset($_COOKIE['username'])) 
            $_SESSION['username'] = $_COOKIE['username'];

所以不要这样做:

setcookie($username, time()-3600);

这样做:

 setcookie($username, "", time()-3600);
相关问题