Home  >  Article  >  Backend Development  >  Introduction to the usage of PHP setcookie() function_PHP tutorial

Introduction to the usage of PHP setcookie() function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:14:47937browse

Cookies are just like sessions in php, except that one is on the client side and the other is on the server side. Let me introduce the cookie setting and deletion code of setcookie in php in detail.

setcookie() syntax

setcookie (PHP 3, PHP 4, PHP 5)

setcookie -- send a cookie message

Description: bool setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure]]]]] )

Example

Write cookie

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
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);
?>

Copy code

 代码如下 复制代码

// Print an individual cookie
echo $_COOKIE["TestCookie"];
echo $HTTP_COOKIE_VARS["TestCookie"];

// Another way to debug/test is to view all cookies
print_r($_COOKIE);
?>

$value = 'something from somewhere';

setcookie("TestCookie", $value);
代码如下 复制代码

// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", "example.com", 1);
?>

setcookie("TestCookie", $value, time()+3600); /* expire in 1 hour */

setcookie("TestCookie", $value, time()+3600, "/~rasmus/", "example.com", 1);
?>

 代码如下 复制代码

setcookie(’name’, ‘jeff’);

echo “Hello Everyone!”;

?>

 代码如下 复制代码

echo “Hello Everyone!”;

setcookie(’name’, ‘jeff’);

?>

Read cookie
The code is as follows
Copy code

// Print an individual cookie echo $HTTP_COOKIE_VARS["TestCookie"]; // Another way to debug/test is to view all cookies print_r($_COOKIE); ?> Delete cookies

The code is as follows Copy code
// set the expiration date to one hour ago<🎜> setcookie ("TestCookie", "", time() - 3600);<🎜> setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", "example.com", 1);<🎜> ?> Because of the way cookies work with HTTP, 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 the right thing to do:
The code is as follows Copy code
<🎜>setcookie(’name’, ‘jeff’);<🎜> <🎜>echo “Hello Everyone!”;<🎜> <🎜>?> The following is incorrect:
The code is as follows Copy code
<🎜>echo “Hello Everyone!”;<🎜> <🎜>setcookie(’name’, ‘jeff’);<🎜> <🎜>?> http://www.bkjia.com/PHPjc/628948.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628948.htmlTechArticleCookies are like sessions in php, except that one is on the client side and the other is on the server side. Below I Let’s introduce the cookie setting and deletion code of setcookie in php in detail. setcookie() syntax...