For example:
<?php
$USERID="PHPer";
$CookieTime=0;
setcookie("USERID", "$USERID", time()+$CookieTime,"/","");
?>
You will find that after this statement is executed, there is nothing in the cookie, and when you go to the next page, you will display the value of the COOKIE variable $USERID.
The problem analysis is as follows:
First, set the value of "Cookie Expiration Time" of the browser process is not the current Unix timestamp +0. If it is set to the browser process, just set the expiration time to 0.
Secondly, it is not clear what domain name you use when testing this page. If you set "", it means that you must use "" to access the cookie to be valid. In fact, if many of your domain names visit this page, then this place can be empty or the domain names that access this cookie are all under the same domain, then set to "." Remember to have a "dot" in front.
The above program may work if you write this way:
<?php
$USERID="PHPer";
$CookieTime=0;
setcookie("USERID", "$USERID", 0,"/","");
echo (isset($_COOKIE['USERID']) ? $_COOKIE['USERID'] : '');
?>
There will be no output when you open this page for the first time, because the cookie will not take effect immediately on the current page.
It will be displayed after refreshing.
Copy the codeThe code is as follows:
<?php
$USERID="PHPer";
$CookieTime=0;
setcookie("USERID", "$USERID", time()+$CookieTime,"/","");
?>
You will find that after this statement is executed, there is nothing in the cookie, and when you go to the next page, you will display the value of the COOKIE variable $USERID.
The problem analysis is as follows:
First, set the value of "Cookie Expiration Time" of the browser process is not the current Unix timestamp +0. If it is set to the browser process, just set the expiration time to 0.
Secondly, it is not clear what domain name you use when testing this page. If you set "", it means that you must use "" to access the cookie to be valid. In fact, if many of your domain names visit this page, then this place can be empty or the domain names that access this cookie are all under the same domain, then set to "." Remember to have a "dot" in front.
The above program may work if you write this way:
Copy the codeThe code is as follows:
<?php
$USERID="PHPer";
$CookieTime=0;
setcookie("USERID", "$USERID", 0,"/","");
echo (isset($_COOKIE['USERID']) ? $_COOKIE['USERID'] : '');
?>
There will be no output when you open this page for the first time, because the cookie will not take effect immediately on the current page.
It will be displayed after refreshing.