doushi7761 2011-01-06 15:48
浏览 24
已采纳

设置和阅读Cookie的问题,PHP

Greetings,

Been going around in circles trying to figure out why this will not work. Making a low security log-in system using cookies due to an issue with sessions on the device being used. The set cookie works on its own but either is not setting properly in this script or is not being read properly on the auth script. Also, after the cookie should be set, it is not in the browser. Ideas??

Login

   <?php
    //If passwords match, a cookie is created
    if ($pw = $hashedpw) {
         $memberID = "1221"; //Pulled from DB
         setcookie('MDADMIN_SESS_ID',$memberID,'0','', '.somewhere.com');
        header('Location: http://somewhere.com/secure_page.php');
    }
    ?>

Auth

<?php
//Verify that cookie is present
$cookie = $_COOKIE['MDADMIN_SESS_ID'];
if(!isset($cookie)) {
        header("Location: http://somewhere.com/failed.php");
        exit();
}
?>

The process is as follows: Login Form -> Login Script -> Secure Page (if passwords match) -> Auth Script checked (via include) -> redirect to failed login if cookie not present. When run, it always defaults to the cookie not being present, even though the login script correctly directs to the secure page (logged in successfully).

  • 写回答

2条回答 默认 最新

  • dongshungai4857 2011-01-06 15:54
    关注

    try

    <?php
    //If passwords match, a cookie is created
        if ($pw = $hashedpw) {
            $memberID = "1221"; //Pulled from DB
            setcookie('MDADMIN_SESS_ID',$memberID,'0','/', '.somewhere.com');
            header('Location: http://somewhere.com/secure_page.php');
            exit();
        }
    ?>
    

    You are missing a / for the path.

    Also make sure you have an exit(); function after the header; because if you unset the cookie later at someplace then it might also get affect.

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘