开发者

Help fixing this redirecting problem

On index.php, I want to redirect users that are logged in to home.php ana also check if a cookie is set and redirect them if the cookie is storing the correct info. But the problem is, if a user saved a cookie and wants to logout and go to the index page, he can't since the index page will re-c开发者_如何学Check his saved cookie and re-sign him in. Please help:

<?php

include("checkcook.php");

function redirect() {
    header('location:home.php');
    die();
}

session_start();

if((isset($_COOKIE["jmuser"])) && (isset($_COOKIE["jmpass"]))){
    checkCookie($_COOKIE["jmuser"], $_COOKIE["jmpass"]);
    } 

if(isset($_SESSION['username'])){
            redirect();
        }
?>

Oh and here is checkcook.php

<?php
    function checkCookie($username, $password){

        $conn=mysql_connect("localhost", "***", "***") or die(mysql_error());
        mysql_select_db('jmtdy', $conn) or die(mysql_error());

        $result=mysql_query("select * from users where username = '$username'");
        if($result != false){

            $username=mysql_real_escape_string($username);
        $password=mysql_real_escape_string($password);

            $dbArray=mysql_fetch_array($result);
            $dbArray['password']=mysql_real_escape_string($dbArray['password']);
            $dbArray['username']=mysql_real_escape_string($dbArray['username']);

        if(($dbArray['password'] != $password ) || ($dbArray['username'] != $username) || ($dbArray['active'] != '1')){
                setcookie("jmuser","",time()-3600);
                setcookie("jmpass","",time()-3600);
                session_unset($_SESSION['username']);
                session_unset($_SESSION['password']);
                return;
                }
                $_SESSION['username']=$username;
                return true;
               }

            else{

                setcookie("jmuser","",time()-3600);
                setcookie("jmpass","",time()-3600);
                session_unset($_SESSION['username']);
                return;
            }
        }

?>          

and this is my logout code. But if I destroy the cookie, then the user would need to sign in every time he/she accesses the website? I don't want that.

<?php

function redirect() {
    header('location:index.php');
    die();
} 
session_start();
session_destroy();
redirect();

?>


Simple solution, just remove the cookie when they logout

//sets the expiration time to -3600 seconds, i.e. 1 hour in the past

setcookie ("AnExampleCookie", "", time() - 3600);

Or are you saying, if they want to logout from home.php, they can't because it redirects? If that's the case then maybe implement a way of knowing if the user has already been redirected from home.php once by setting a counter.

Ok you have made the question a bit clearer now

You say

"But if I destroy the cookie, then the user would need to sign in every time he/she accesses the website? I don't want that."

But is the user not choosing to logout?

"if a user saved a cookie and wants to logout and go to the index page"

So I still say, just remove the cookie when they logout, before they reach the home.php page

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜