How to create a Cookie in php

Source: Internet
Author: User
Cookie Creation is very simple. as long as your browser supports the Cookie function, you can use the built-in functions of PHP to create a new Cookie. Use the setcookie () function in PHP to create a Cookie. Cookie Creation is very simple. as long as your browser supports the Cookie function, you can use the built-in functions of PHP to create a new Cookie. In PHP Setcookie ()Function to create a Cookie. Before creating a Cookie, you must understand that the Cookie is a part of the HTTP header, and the header must be sent before other content on the page. Therefore, it must be output first. Therefore, do not output data before calling the setcookie () function even if it is a space or empty line. If an HTML tag, echo statement, or even a blank line is output before the setcookie () function, a program error occurs.

The syntax is as follows:

 setcookie(name,value,expire,path,domain,secure)

The setcookie () function defines a Cookie sent together with other HTTP headers. all its parameters correspond to the attributes of the HTTP header Cookie data. Although the setcookie () function does not have many import parameters, other parameters except the parameter name are not required, and we often use only $ name, $ value and $ expire parameters.

The parameters of the setcookie () function are described as follows:

1) name

Cookie variable name

You can call the COOKIE named cookiename through $ _ Cookie ["cookiename "].

2) value

Cookie variable value, which is stored on the client and cannot be used to store sensitive data

You can use $ _ COOKIE ["values"] to obtain the value Named values.

3) expire

Cookie Expiration time. expire is a standard UNIX time mark. it can be obtained using the time () function or mktime () function, in seconds.

If you do not set the Cookie expiration time, the Cookie will always be valid unless you manually delete it.

4) path

Valid Cookie path on the server

If this parameter is set to "/", it is valid in the entire domain. if it is set to "/11", it is valid in the/11 directory and subdirectory under the domain. The current directory is used by default.

5) domain

Cookie valid domain name

To make the Cookie valid for all subdomain names under the abc.com domain name, set it to abc.com

6) secure

Indicates whether the Cookie uses secure HTTPS. The value is 0 or 1.

If the value is 1, the Cookie is valid only on the HTTPS connection. if the value is 0, the Cookie is valid on both the HTTP and HTTPS connections.

If only $ name is used, the cookie option of the original name will be deleted. you can also use an empty string to omit this parameter. The $ expire and $ secure parameters are an integer. you can use 0 to omit this parameter, rather than an empty string. However, the $ expire parameter is a regular UNIX time integer that is returned by the time () or mktime () function. The $ secure parameter indicates that this Cookie will be sent only when a secure HTTPS connection is established.

Use the setcookie () function to set all parameters. the instance code is shown as follows:

 

Note: In the preceding example, a Cookie named "username" is created. its content value is the string "sky", and its storage on the client is valid for one hour. The parameter "/test" indicates that the Cookie is valid only in this subdirectory or subdirectory of the weapon. The ".php1.cn" parameter allows cookies to be valid in all subdomains under a domain name such as php1.cn. although "." is not required, it is compatible with more browsers. When the last parameter is set to 1, the Cookie can only be set in a secure connection.

The value given by setcookie () can only be a number or string, and cannot be other complex structures.

The above describes how to create cookies in php. For more information, see other related articles in the first PHP community!

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.