简体   繁体   English

似乎未设置PHP cookie superglobal

[英]PHP cookie superglobal does not seem to be set

I am currently developing a PHP website. 我目前正在开发一个PHP网站。 I am trying to set a cookie which will store a user id. 我正在尝试设置一个将存储用户ID的cookie。 This is for a logging in system and I need the user id to be able to add it into different tables in a MySQL database. 这是用于登录系统的,我需要用户ID才能将其添加到MySQL数据库的不同表中。

When the user successfully logs in it creates a PHP session and a cookie and then uses a meta tag refresh to direct to the index page. 当用户成功登录后,它会创建一个PHP会话和一个cookie,然后使用元标记刷新来定向到索引页面。

if ($row = mysql_fetch_array($result))
{
    setcookie('id', $row['use_id']);
    $_SESSION['user'] = $username;
    setcookie('userID', $row['use_id']);
    echo '<meta http-equiv="refresh" content="0;url=../index.php">';
}

If I remove the meta refresh and just echo the cookie using $_COOKIE['userID'] it works fine but when I try and use $_COOKIE['userID'] on the index.php page after it has done the meta refresh it isn't displaying anything. 如果删除元刷新并仅使用$_COOKIE['userID']响应cookie,它就可以正常工作,但是当我尝试在完成元刷新后在index.php页面上使用$_COOKIE['userID']时,它不是什么也没显示。

setcookie('userID', $row['use_id'], 0, "/");

Edit: zero for expire time. 编辑:过期时间为零。

Your cookie is not visible for index.php because it has been set from script in subdirectory. 您的cookie对于index.php不可见,因为它是通过子目录中的脚本设置的。 So it is not visible for pages on upper levels. 因此,它对于较高级别的页面不可见。 You should set cookie path (4th parameter) to "/" and the cookie will be available within the entire domain. 您应该将cookie路径(第4个参数)设置为“ /”,并且cookie将在整个域中可用。

This was the reason why your cookie was not visible to index.php. 这就是为什么您的cookie对index.php不可见的原因。

header('Location: ../index.php');
                  ^^^

You have redirected page to upper directory level, but your cookie was not set for entire domain. 您已将页面重定向到上一级目录,但未为整个域设置cookie。

if the index pages directory is upper than the directory which you set the cookie from, the cookie will not be available in the index page. 如果索引页面目录比您设置cookie的目录更高,则该cookie在索引页面中将不可用。 And i suggest you to use location header for redirections. 而且我建议您使用位置标头进行重定向。 Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM