Home  >  Article  >  Backend Development  >  How to use PHP to manipulate cookies_PHP tutorial

How to use PHP to manipulate cookies_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:39696browse

The PHP setcookie() function sends an HTTP cookie to the client. A cookie is a variable sent to the browser by the server. Cookies are typically small text files that a server embeds on a user's computer. This cookie is sent each time the computer requests a page through the browser. The name of the cookie is specified as a variable of the same name. For example, if the cookie being sent is named "name", a variable named $user is automatically created containing the cookie's value.

The cookie must be assigned before any other output is sent. The function returns true if successful, false otherwise.

setcookie(name, value, expire, path, domain, secure)
  • name required. Specifies the name of the cookie.
  • value required. Specifies the value of the cookie.
  • expire Optional. Specifies the validity period of the cookie.
  • path optional. Specifies the server path for cookies.
  • domain optional. Specifies the domain name for the cookie.
  • secure Optional. Specifies whether cookies are transmitted over a secure HTTPS connection.

The value of the cookie named "user" can be accessed via $HTTP_COOKIE_VARS["user"] or $_COOKIE["user"]. When sending a cookie, the cookie value is automatically URL-encoded. URL decoding is done on reception. If you don't need this, you can use setrawcookie() instead.

Procedure 1:

Set and send cookies:

<?php
$value = "my cookie value";
// 发送一个简单的 cookie
setcookie("TestCookie",$value);
?>
<html>
<body>
...
...
<?php
$value = "my cookie value";
// 发送一个 24 小时候过期的 cookie
setcookie("TestCookie",$value, time()+3600*24);
?>
<html>
<body>

Procedure 2:

Different ways to retrieve cookie values:

<html>
<body>
<?php
// 输出个别的 cookie
echo $_COOKIE["TestCookie"];
echo "<br />";
echo $HTTP_COOKIE_VARS["TestCookie"];
echo "<br />";
// 输出所有 cookie
print_r($_COOKIE);
?>
</body>
</html>

Program output:

my cookie value
my cookie value
Array ([TestCookie] => my cookie value)

Procedure 3:

Delete a cookie by setting the expiration date to a date/time in the past:

<?php
// 把失效日期设置为一小时前
setcookie ("TestCookie", "", time() - 3600);
?>

Procedure 4:

Create an array of cookies:

<?php
setcookie("cookie[three]","cookiethree");
setcookie("cookie[two]","cookietwo");
setcookie("cookie[one]","cookieone");
// 输出 cookie (在重载页面后)
if (isset($_COOKIE["cookie"]))
{
  	foreach ($_COOKIE["cookie"] as $name => $value)
	{
    	echo "$name : $value <br />";
    }
}
?>

Program output:

three : cookiethree
two : cookietwo
one : cookieone

Procedure 5:

/**
 * 01.cookie设置
 * */
function ssetcookie($var, $value, $life=0) {
    global $_SGLOBAL, $_SC, $_SERVER;
    setcookie($_SC['cookiepre'].$var, $value, $life?($_SGLOBAL['timestamp']+$life):0, $_SC['cookiepath'], $_SC['cookiedomain'], $_SERVER['SERVER_PORT']==443?1:0);
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752449.htmlTechArticlePHP setcookie() function sends an HTTP cookie to the client. A cookie is a variable sent to the browser by the server. A cookie is typically a small text file that a server embeds on a user's computer...
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