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条)

报告相同问题?

悬赏问题

  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法