简体   繁体   中英

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. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. Cookie values also exist in $_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 (or $HTTP_COOKIE_VARS ) contains the cookie information that was sent by the client together with the request . Since you just set the cookie, the client will only be able to send the information on the next request.

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.


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. 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.

What you really shouldn't be doing is storing the admin name and password in a cookie. It's Bad™ . Anyone with access to the machine could just look at the cookie to get sensitive login information. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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