简体   繁体   English

无法在PHP中设置Cookie?

[英]Can't set cookies in PHP?

由于某种原因,此php脚本不会回显任何内容:

<?php setcookie("pop",'hi',time()+604800); echo $HTTP_COOKIE_VARS['pop']; ?>

自PHP 4.1.0起,不建议使用$HTTP_COOKIE_VARS ,请尝试使用$_COOKIE['pop']

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. 一旦设置了cookie,就可以使用$ _COOKIE或$ HTTP_COOKIE_VARS数组在下一页加载时访问它们。 Note, superglobals such as $_COOKIE became available in PHP 4.1.0. 注意,诸如$ _COOKIE之类的超全局变量在PHP 4.1.0中可用。 Cookie values also exist in $_REQUEST. Cookie值也存在于$ _REQUEST中。

via Documentation . 通过文档

It might be that you can access it on the same page load. 您可能可以在同一页面加载时访问它。

When you set a cookie, its value will be send to the client together with the page. 设置Cookie时,其值将与页面一起发送到客户端。 $_COOKIE (or $HTTP_COOKIE_VARS ) contains the cookie information that was sent by the client together with the request . $_COOKIE (或$HTTP_COOKIE_VARS )包含客户端与请求一起发送的cookie信息。 Since you just set the cookie, the client will only be able to send the information on the next request. 由于您只是设置Cookie,因此客户端只能在下一个请求时发送信息。

The manual puts it like this : 手册说得像这样

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. 一旦设置了cookie,就可以使用$ _COOKIE或$ HTTP_COOKIE_VARS数组在下一页加载时访问它们。


Re EDIT 重新编辑

It's still the same problem. 仍然是同样的问题。 If the name and password are that of the admin, you're just setting a cookie, which happens silently. 如果名称和密码是管理员的名称和密码,则只需设置一个cookie,它会以静默方式发生。 Then, you're echoing the contents of the cookie that were sent with the request, which is probably empty, so it doesn't echo anything. 然后,您将回显与请求一起发送的cookie的内容,该内容可能为空,因此它不会回显任何内容。

What you really shouldn't be doing is storing the admin name and password in a cookie. 您真正不应该做的是将管理员名称和密码存储在cookie中。 It's Bad™ . 这是Bad™ Anyone with access to the machine could just look at the cookie to get sensitive login information. 有权访问该计算机的任何人都可以查看cookie以获取敏感的登录信息。 Furthermore, the information will be transmitted with every request, most likely in plain text, so any proxy or sniffer can pick up the login information as well. 此外,该信息将与每个请求一起发送,很可能以纯文本形式发送,因此任何代理或嗅探器也可以获取登录信息。 Either encrypt them before storing them in the cookie or use proper sessions. 在将它们存储在Cookie中之前对其进行加密,或者使用适当的会话。

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

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