PHP Setcookie (name, value, expires, path, domain, secure) Parameters _php Tutorial

Source: Internet
Author: User
Tags setcookie
Setcookie () defines a cookie that is sent along with the rest of the HTTP headers. Like other headers, a cookie must be sent before any other output from the script (this is a protocol restriction). This requires that the call to this function be placed before any output, includingAndLabel and any spaces. If there is any output before calling Setcookie (), this function will fail and return FALSE. Returns TRUE if the Setcookie () function runs successfully. This does not indicate whether the user has accepted the cookie.
function Definition:
BOOL Setcookie (string name [, string value [, int expire [, String path [, string domain [, bool secure]]])
Setcookie () parameter explanation
The
Parameters Description Example
Name The name of the cookie Use $_COOKIE['cookiename'] call a cookie named CookieName.
Value The value of the cookie, stored in the client, do not store sensitive data assume name that is ' cookiename ', can be passed $_COOKIE['cookiename'] gets its value.
expire

The time that the Cookie expires.  This is a UNIX timestamp, which is the number of seconds since the Unix era.

in other words, usually with time () The function, plus the number of seconds, sets the expiration period of the cookie.

or with mktime () to implement.

time () +60*60*24*30 expires 30 days after setting the cookie.

if not set, the cookie will expire at the end of the session (usually the browser is closed).

path Cookie is a valid path on the server side.

If the parameter is set to , the cookie is in the entire domain valid,

If set to '/foo/' , The cookie is valid only in the /foo/ directory and its subdirectories under the domain , for example /foo/bar/.

Domain The domain name that the cookie is valid for.

This parameter should be set to enable the cookie to be valid for all subdomains, such as the example.com domain name '.example.com' .

Although . not required, it will be compatible with more browsers.

If this parameter is set www.example.com , it is only www valid within the subdomain.

See the tail matching in the cookie specification for details.

Secure

Indicates whether the cookie is transmitted only through a secure HTTPS connection.

when set into TRUE , the cookie is only set in a secure connection. The default value is FALSE.

0Or1

Example 1. Setcookie () Send example
Copy CodeThe 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/", ". utoronto.ca", 1);

Note that the portion of the value in the cookie is automatically encoded with UrlEncode when it is sent and automatically decoded when it is received and assigns the value to the cookie variable with the same name. If you do not want to do this and use PHP 5, you can use Setrawcookie () instead. The following simple example can get the value of the cookie you just set:
Copy CodeThe code is as follows:
Output a separate cookie
echo $_cookie["TestCookie"];
echo $HTTP _cookie_vars["TestCookie"];
Another way to debug is to output all cookies
Print_r ($_cookie);
?>

To delete a cookie, you need to ensure that its expiration period is in the past before it triggers the browser's removal mechanism. The following example shows how to delete a cookie that you just set:
Example 2. Setcookie () Delete Example
Copy CodeThe code is as follows:
Set the expiration time to one hour ago
Setcookie ("TestCookie", "", Time ()-3600);
Setcookie ("TestCookie", "", Time ()-3600, "/~rasmus/", ". utoronto.ca", 1);

You can also set an array cookie by using an array symbol in the cookie name, you can set multiple cookies as the array unit, and all the values in the script to extract the cookie are placed in an array type:
Example 3. Examples of arrays used in Setcookie ()
Copy CodeThe code is as follows:
Set cookies
Setcookie ("Cookie[three]", "Cookiethree");
Setcookie ("Cookie[two]", "cookietwo");
Setcookie ("Cookie[one]", "Cookieone");
After the page is refreshed, it appears
if (Isset ($_cookie[' COOKIE ")) {
foreach ($_cookie[' COOKIE '] as $name = = $value) {
echo "$name: $value
\ n ";
}
}
?>

The example above will output:
Three:cookiethree
Two:cookietwo
One:cookieone

Summary: The basic use of cookies is not difficult, the main focus of this article is to master path and domain settings.


http://www.bkjia.com/PHPjc/327934.html www.bkjia.com true http://www.bkjia.com/PHPjc/327934.html techarticle Setcookie () defines a cookie that is sent along with the rest of the HTTP headers. Like other headers, a cookie must be sent before any other output from the script (this is a protocol restriction). This ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.