PHP Setcookie (name, value, expires, path, domain, secure) parameters detailed

Source: Internet
Author: User
Tags bool constant function definition set cookie setcookie valid in domain

Setcookie () defines a cookie that is sent along with the rest of the HTTP headers. As with other headers, cookies must be sent before any other output of the script (this is the protocol limit). This requires that the call to this function be placed before any output, including the function Definition:
bool Setcookie (string name [, string value [, int expire [, String path [, string domain [, bool secure]]]
Setcookie () parameter detailed

Parameters Description Example
Name The name of the cookie Use $_COOKIE['cookiename'] invokes a cookie named CookieName.
Value The value of the cookie, stored on the client, do not store sensitive data assumes name that is ' cookiename ', can pass $_COOKIE['cookiename'] gets its value.
Expire

Time the Cookie expires. This is a Unix timestamp, that is, the number of seconds from the start of the Unix era.

In other words, it's usually Time () function plus the number of seconds to set the expiration period of the cookie.

or use mktime () to achieve.

time()+60*60*24*30The cookie will be set to expire after 30 days.

If it is not set, the cookie will expire after the session is closed (typically browser shutdown).

path cookies are valid paths on the server side. if the parameter is set to "", the cookie is in the entire domain

if set to '/foo/' , cookies are only available in domain /foo/ directory and its subdirectories are valid, such as /foo/bar/ . The default value of the

is the current directory where the cookie is set.

Domain A valid domain name for this cookie.

To make a cookie valid for all subdomains, such as the example.com domain name, this parameter should be set to '.example.com' .

Although . not necessary, but plus it will be compatible with more browsers.

If this argument is set www.example.com , it is only www valid within the child domain.

Details see the tail matching in the cookie specification.

Secure

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

when set into TRUE , cookies are set only in secure connections. The default value is FALSE.

0Or1


Example 1 Setcookie () Send example

Copy Code code 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 assigned the value to a cookie variable of its own name. If you don't want to do this and you're using PHP 5, you can use Setrawcookie () instead. Here's a simple example that gets the value of the cookie you just set:

Copy Code code as follows:


<?php


//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 remove a cookie, you need to ensure that its expiration period is in the past in order to trigger the browser's deletion mechanism. The following example shows how to delete a cookie that you just set:
Example 2 setcookie () Delete Example

Copy Code code as follows:


//Set the expiration time to an 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, and you can set multiple cookies as array cells, and all values are placed in an array when the script extracts the cookie:
Examples of using arrays in Example 3 Setcookie ()

Copy Code code as follows:


<?php


//Set Cookie


Setcookie ("Cookie[three]", "Cookiethree");


Setcookie ("Cookie[two]", "cookietwo");


Setcookie ("Cookie[one]", "Cookieone");


//Refresh the page, display it


if (isset ($_cookie[' COOKIE ')) {


foreach ($_cookie[' COOKIE '] as $name => $value) {


echo "$name: $value <br/>n";


}


}


?>


The previous example will output:
three:cookiethree
two:cookietwo
One:cookieone

Summary: The basic use of cookies is easy, and this article records the main focus is to master path settings and domain name settings in domain.

Related Article

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.