PHP analysis of cookies and session

Source: Internet
Author: User
Tags bool current time header setcookie valid

Cookies for 1.PHP

A cookie is a mechanism for storing data on a remote browser side to track and identify the user.

PHP sends cookies in the header information of the HTTP protocol, so the Setcookie () function must be called before other information is exported to the browser, similar to the limit on the header () function.

1.1 Setting Cookies:

You can use the Setcookie () or Setrawcookie () function to set cookies. You can also set it by sending the HTTP headers directly to the client.

1.1.1 Use the Setcookie () function to set cookies:

BOOL Setcookie (string name [, string value [, int expire [, String path [, string domain [, bool secure [, BOOL HttpOnly ]]]]]] )

Name:cookie variable Name

The value of the Value:cookie variable

Expire: At the end of the validity period,

Path: Valid directory,

Domain: Valid domain name, top-level domain unique

Secure: If the value is 1, the cookie can only be valid on the HTTPS connection, and HTTP and HTTPS are available if the default is 0.

Example:

<?php
$value = 'something from somewhere';setcookie("TestCookie", $value); /* 简单cookie设置 */
setcookie("TestCookie", $value, time()+3600); /* 有效期1个小时 */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1); /* 有效目录 /~rasmus,有效域名example.com及其所有子域名 */
?>

Set multiple cookie variables: Setcookie (' var[a] ', ' value '); The variable is represented by an array, but his subscript is not quoted. This allows you to read the cookie variable with $_cookie[' var ' [' a '].

1.1.2. Use header () to set cookies;

Header ("Set-cookie:name= $value [;p ath= $path [;d omain=xxx.com[; ...]]");

The following parameters are the same as those listed above for the Setcookie function.

Like what:

$value = 'something from somewhere';
header("Set-Cookie:name=$value");

1.2 Cookie reads:

Using PHP's built-in Super global variable $_cookie can read cookies on the browser side.

In the example above, the cookie "TestCookie" is set, and now we read:

Print $_cookie[' TestCookie '];

Is the cookie being exported?!

1.3 Deleting cookies

Simply set the valid time to less than the current time and leave the value blank. For example:

Setcookie ("name", "", Time ()-1);

Similar with header ().

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.