Dynamic web skills PHP analysis on cookies and Sessions

Source: Internet
Author: User
1. COOKIEcookie of PHP is a mechanism for storing data on a remote browser to track and identify users. PHP sends a cookie in the http header. Therefore, the setcookie () function must be called before other information is output to the browser.

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. This 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 apply 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:
$ Value = 'something from somewhere ';

Setcookie ('testcookie ', $ value);/* simple cookie settings */
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. apply header () to set cookie;
Header ('set-Cookie: name = $ value [; path = $ path [; domain = ***. 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 ');

1.2 Cookie reading:

Directly use php's built-in Super global variable $ _ COOKIE to read the cookie of the browser.
The cookie 'testcookie 'is set in the preceding example. now we can read:

Print $ _ COOKIE ['testcooker'];

Is the COOKIE output ?!

1.3 Delete a cookie
Set the effective time to less than the current time, and set the value to null. for example:
Setcookie ('name', '', time ()-1 );
Similar to header.

1.4 solution to common titles:

1) an error message is prompted when setcookie () is used. it may be because there is an output or space before setcookie () is called. it is also possible that your document is converted from other character sets. the document may be followed by a BOM signature (that is, add some hidden BOM characters in the file content ). the solution is to prevent your documents from presenting this situation. you can also use the ob_start () function.
2) $ _ COOKIE is affected by magic_quotes_gpc and may be escaped actively.
3) during application, it is necessary to test whether the user supports cookies.

1.5 cookie working mechanism:

Some learners are excited and have no idea about the principle, so I put it behind me.
A) the server sends an http Set-Cookie header in response and sets a cookie in the client (multiple cookies require multiple heads ).

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.