doujiao2014 2013-02-12 13:15
浏览 42
已采纳

php设置会话与cookie

$_SESSION['user_id'] = $login;  
setcookie('user_id', '$login' , time()+86000);
header('Location: userindex.php');

function logged_in() {
return (isset($_SESSION['user_id']) || isset($_COOKIE['user_id']) ? true : false;
}

I have SESSION but I wonna include COOKIE too but I don't know how to restart SESSION with COOKIE. I don't have a idea how I can get that. I create COOKIE but can't logout and have problem with SESSION somebody can help me to fix my problem???? And in every page on top I have logged_in function for check if user is logged in or not I wonna these logged_in function to check if user has cookie to auto login to user cookie. I think it is in logged_in function must get write some code and...

  • 写回答

3条回答 默认 最新

  • doudongdang4483 2013-02-12 13:28
    关注

    I will note that this is not secure, as any one can create the cookie, using something like firebug.

    @session_start();
    
    function logged_in() {
        if(!isset($_SESSION['user_id']) && isset($_COOKIE['user_id'])) {
            $_SESSION['user_id'] = $_COOKIE['user_id'];
        }
        return isset($_SESSION['user_id']);
    }
    
    function logout() {
        unset($_SESSION['user_id']);
        setcookie("user_id", "", time() - 3600);
        header("Location: http://".$_SERVER['HTTP_HOST']);
        exit;
    }
    

    Edit: Added logout() - will remove both session and cookie 'user_id', then redirect to homepage

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?