Cookie and session Analysis in PHP

Source: Internet
Author: User
1. php cookie
Cookie is a mechanism for storing data in a remote browser and tracking and identifying users.
PHP sends cookies in the HTTP header. Therefore, the setcookie () function must be called before other information is output to the browser, which is similar to the header () function.
1.1 set COOKIE:
You can use the setcookie () or setrawcookie () function to set the cookie. You can also set it by sending an HTTP header directly to the client.
1.1.1 use the setcookie () function to set the cookie:
Bool setcookie (string name [, string value [, int expire [, string path [, string domain [, bool secure [, bool HTTPOnly])
Name: Cookie variable name
Value: the value of the cookie variable.
Expire: the end time of the validity period,
Path: valid directory,
Domain: Valid domain name, unique in top-level domain
Secure: If the value is 1, the cookie can only be valid for HTTPS connections. If the default value is 0, both HTTP and HTTPS can be used.
Example:
<? PHP
$ Value = 'something from somewhere ';
Setcookie ("testcookie", $ value);/* simple cookie setting */
Setcookie ("testcookie", $ value, time () + 3600);/* valid for 1 hour */
Setcookie ("testcookie", $ value, time () + 3600 ,"/~ Rasmus/"," .example.com ", 1);/* valid directory /~ Rasmus, valid domain name example.com and all its subdomains */
?>
Set multiple COOKIE variables: setcookie ('var [a] ', 'value'); use an array to represent variables, but its subscript is not enclosed in quotation marks. in this way, you can use $ _ cookie ['var'] ['a'] to read the cookie variable.
1.1.2. Use header () to set the cookie;
Header ("Set-COOKIE: name = $ value [; Path = $ path [; domain = xxx.com [;...]");
The following parameters are the same as those listed in the setcookie function above.
For example:
$ Value = 'something from somewhere ';
Header ("Set-COOKIE: name = $ value ");*
......
[Read the full text]

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.