简体   繁体   English

如何在PHP中修复Cookie?

[英]How can i fix cookies in PHP?

i want to include cookies in my website in php but i don't know how to do this. 我想在PHP的网站中包含cookie,但是我不知道该怎么做。

i've tried to include them with sessions but it didn't work. 我试图将他们包括在会议中,但没有成功。

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

i expected to keep me connected but it didn't. 我希望保持联系,但事实并非如此。 What else do i need to add in my code to make this work? 为了使这项工作有效,我还需要在代码中添加什么?

You're looking for setcookie() 您正在寻找setcookie()

Take a look at the PHP documentation for examples. 查看示例的PHP文档。

Edit: 编辑:

To explain, before you can read a cookie, you need to set the cookie somewhere. 为了说明,在读取cookie之前,需要将cookie设置在某个地方。 You must call setcookie() before you output any information (eg echo or html before the php script tags) or there will be an error. 在输出任何信息之前(例如,在php脚本标签之前的echo或html setcookie() ,必须调用setcookie() ,否则会出现错误。

setcookie() also does not determine whether a user accepted the cookie. setcookie()也不能确定用户是否接受了cookie。 If a user has cookies disabled in browser, you would not be able to set or retrieve a cookie. 如果用户在浏览器中禁用了cookie,则将无法设置或检索cookie。

Since cookies can be manipulated, they tend to be a very bad place to store whether a user is logged in. Generally, sessions are better for that. 由于可以操作cookie,因此它们通常是存储用户是否已登录的非常糟糕的地方。通常,会话对此更好。

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

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