简体   繁体   English

我的Cookie不会留下(PHP)

[英]My cookies won't stay (PHP)

I'm building an autologin system using cookies, but one fundamental part of the functionality of the cookies fails: they are non-persistent over different sessions - or even pages! 我正在使用cookie构建自动登录系统,但是cookie功能的一个基本部分失败了:它们在不同的会话(甚至页面)上都是非持久的! In my login script, I set the cookies like this: 在登录脚本中,我将cookie设置如下:

setcookie('userID', $userID, time()+86400); // (edited after replies)

$userID has a value. $ userID有一个值。
Then I print the $_COOKIE variable and it says array(['base_usid'] => 1); 然后我打印$ _COOKIE变量,它说array(['base_usid'] => 1); So that's good, but when I click the home page and print the $_COOKIE variable there, it says NULL. 这样很好,但是当我单击主页并在其中打印$ _COOKIE变量时,它显示为NULL。
Does anyone see the problem? 有人看到这个问题吗?

Cookies should have a time value for how long they should stay... Check http://php.net/manual/en/function.setcookie.php Cookies的时间值应为它们应停留的时间...请检查http://php.net/manual/zh/function.setcookie.php

In other words, change it to: setcookie('userID', $userID, time()+86400); 换句话说,将其更改为:setcookie('userID',$ userID,time()+ 86400); to make it stay for a day for example. 例如,使其停留一天。

Aah, I've learned something new about cookies :) They have a path and they are only available on that path (the directory they were created in). 嗯,我学到了一些有关cookie的新知识:)它们有一个路径,并且只能在该路径(它们在其中创建的目录)中使用。 I created the cookies on /user/login, and then tried to read them on /news/index. 我在/ user / login上创建了cookie,然后尝试在/ news / index上阅读它们。 Won't work. 不行
In the past I used to build websites with all files in just one folder (I know it's bad), so I didn't know of this cookie property. 过去,我过去只用一个文件夹来构建所有文件的网站(我知道这很不好),所以我不知道这个cookie属性。 Sorry, I should have read the manual better... 抱歉,我应该更好地阅读该手册...
Thanks for your help! 谢谢你的帮助!

Ps: Typing print_r($_COOOKIE); ps:输入print_r($_COOOKIE); won't speed up debugging. 不会加快调试速度。 :( :(

Cookies need an expiration time. Cookies需要有效时间。 Otherwise they are by default destroyed when a user closes his browser. 否则,默认情况下,当用户关闭浏览器时,它们会被销毁。

Try this instead 试试这个

setcookie("userID", $userID, time()+3600);

This will last for an hour. 这将持续一个小时。 Make the number bigger to have it last longer. 增大数字可使其使用时间更长。

To unset / remove it, change the plus + to a minus - 要取消设置/删除它,请将加号+更改为减号-

:) :)

If its still not working after you've set an expiry time (and you've checked the clocks on server and client are correct) then have you checked that the cookie is being sent? 如果在设置了有效时间后,它仍然不起作用(并且您已经检查了服务器和客户端上的时钟是否正确),那么您是否检查了cookie的发送? Sounds like the problem with 'headers already sent'. 听起来像“标题已发送”的问题。 Which would also imply you have a problem with error reporting / logging. 这也意味着您在错误报告/日志记录方面存在问题。

C. C。

Do you want to learn how to build CMS systems and login managers, or do you want to build an app... ? 您是否要学习如何构建CMS系统和登录管理器,还是要构建应用程序...? Hate to do this, but my answer is : don't build your own login system. 讨厌这样做,但是我的答案是:不要构建自己的登录系统。 Instead, go grab some framework like CodeIgniter, Kohana, or even drupal or Joomla. 取而代之的是去获取诸如CodeIgniter,Kohana甚至drupal或Joomla之类的框架。 If you are building a login system as a learning experience to understand how cookies work/etc, then fine.. go ahead.. as long as you don't plan on putting it into some production site. 如果您正在建立一个登录系统作为学习体验,以了解Cookie的工作原理等,那么就可以了,只要您不打算将其放入某个生产站点即可。 Otherwise, grab a well tested framework and use it. 否则,请抓住一个经过良好测试的框架并使用它。

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

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