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 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办