简体   繁体   中英

setcookie PHP doesn't work in a right way

I have such situation: I do setcookie("bla",md5("bla"),time()+36000) . After this I do see this cookie in the browser but If I will write print_r($_COOKIE) on the server - there will be not exist cookie with key "bla". Any ideas?

here is the listing:

  setcookie("login_cookie",md5($result['user_password']."solt"),time()+36000);
  setcookie("login_info",$result['user_id'],time()+36000);
  header("Location:{$_SERVER['HTTP_REFERER']}");
  exit();

Try the following (set the path argument to the root):

setcookie("login_cookie",md5($result['user_password']."solt"),time()+36000, '/');
setcookie("login_info",$result['user_id'],time()+36000, '/');

I have a feeling you're going out to a different directory in the redirect which is why it's not displayed, of course, I may be wrong.

$_COOKIE is one of the super globals which contain information passed in the HTTP request. You will only see it when a request has been made by a browser which already has the cookie, not directly after having called setcookie() .

Also, in your code example, you appear to be trying to concat using the + operator:

$result['user_password']+"solt"

PHP uses the . operator for concat.

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