Cookie在设置中设置,但无法获取其值

时间:2015-08-24 15:38:25

标签: php cookies

我在登录我的网站时创建了一个cookie,但是如果我使用PHP来检查它是否已设置它会返回false ...如果我进入我的浏览器设置并查看cookie,我可以看到cookie。

这是我的PHP:

<?php
//Start the session
session_start();
$page = $_GET['page'];

//Check if session keys exist $uniqueID = sha1(session_id().$_SERVER['HTTP_USER_AGENT']);
if (isset($_SESSION['user']) && count($_SESSION['user']) == 4) {
    //User is logged in

} else {
    echo "No Session Set<br />";
    //Check for cookie
    if (isset($_COOKIE["keep_login"])) {
        echo "Cookie is set<br />";
        $t = split(".",$_COOKIE["keep_login"]);
        $email = $t[0];
        $validation = $t[1];

        //Cookie set, check it's not been moved
        include "dbconnect.php";
        if ($stmt = $conn->prepare("SELECT session_key FROM users WHERE email=?")) {
            $stmt->bind_param('s', $email);

            //Check password
            if ($stmt->execute()) {
                $stmt->store_result();
                $stmt->bind_result($stored_session);
                $result->fetch();
                $conn->close();
            } else {
                printf("Error message: %s\n", $conn->error);
            }
            echo $stored_session;
        }
    } else {
        echo "No cookie set<br />"; 
    }
}
exit();
?>

这是显示的内容:

  

没有会话集没有设置cookie

这是我的网络浏览器显示的关于cookie的内容:

Name:   keep_login
Content:    martynleeball%40gmail.com.48409b8315b36ca90e51d23c77938997a796445b
Domain: karl.uphero.com
Path:   /php
Send For:   Any kind of connection
Accessible to Script:   Yes
Created:    Monday, 24 August 2015 at 16:30:02
Expires:    Thursday, 21 August 2025 at 16:30:03

以下是设置Cookie的代码:

//Create cookie if session ID is added to database
setcookie("keep_login",($ft_email.".".sha1(session_id().$_SERVER['HTTP_USER_AGENT'])),time()+(10*365*24*60*60));

1 个答案:

答案 0 :(得分:2)

试试这个,

setcookie("keep_login",($ft_email.".".sha1(session_id().$_SERVER['HTTP_USER_AGENT'])),time()+(10*365*24*60*60), "/");

现在已将Cookie的可用性设置为整个域。

阅读材料:

Read the path part.