PHPsetcookie () function usage. A cookie is like a session in php, but one on the client is on the server. next I will introduce in detail the setcookie setting and deletion code in php. The setcookie () syntax cookie is like a session in php, but one is on the server side on the client side. next I will introduce in detail the setcookie setting and deletion code in php.
Setcookie () syntax
Setcookie (PHP 3, PHP 4, PHP 5)
Setcookie -- send a cookie
Note: bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure])
Example
Write cookie
| The code is as follows: |
|
|
$ Value = 'something from somewhere ';
Setcookie ("TestCookie", $ value );
Setcookie ("TestCookie", $ value, time () + 3600);/* expire in 1 hour */
Setcookie ("TestCookie", $ value, time () + 3600 ,"/~ Rasmus/"," example.com ", 1 );
?> |
Read cookie
| The code is as follows: |
|
|
// Print an inpidual cookie
Echo $ _ COOKIE ["TestCookie"];
Echo $ HTTP_COOKIE_VARS ["TestCookie"];
// Another way to debug/test is to view all cookies
Print_r ($ _ COOKIE );
?> |
Delete cookie
| The code is as follows: |
|
|
// Set the expiration date to one hour ago
Setcookie ("TestCookie", "", time ()-3600 );
Setcookie ("TestCookie", "", time ()-3600 ,"/~ Rasmus/"," example.com ", 1 );
?> |
Because of the specific cookie and HTTP methods, you must send all cookies before you output any text. Otherwise, PHP will give a warning and the cookie will not be sent. Therefore, this is correct:
| The code is as follows: |
|
|
Setcookie ('name', 'Jeff ');
Echo "Hello Everyone !";
?> |
The following is an error:
| The code is as follows: |
|
|
Echo "Hello Everyone !";
Setcookie ('name', 'Jeff ');
?> |
Bytes. Setcookie () syntax...