PHP Session Operation cookie Usage analysis _php tips

Source: Internet
Author: User
Tags php session setcookie

The example of this article analyzes the PHP cookie usage. Share to everyone for your reference, specific as follows:

Session Technology: Cookies

Technology that allows server-side scripting to store data in a browser,
Allows the server to send instructions to the browser to manage cookie data stored on the browser side
If the browser stores cookie data stored by a server, it will bring the cookie data on request

Increase and change
Setcookie (key,val);
Delete
Setcookie (key, "");
Get the COOKIE data that the browser is carrying
$_cookie[key]

Characteristics:

Validity:

The default is a temporary cookie, also known as a session cookie, which is cleared when the session ends (browser shutdown). You can determine the lifetime by setting the timestamp (starting in the first second of 1970), and the Greenwich Mean Time (GMT) as the standard for browser-side time

Setcookie (Key,val,time () +60);
Representative to save 1 minutes, browser will check whether the failure
Setcookie (key,val,0);
Default session
Setcookie (key, ', Time ()-1);
Delete Cookie
Setcookie (key,val,php_int_max);
Logically represents permanent validity.

Valid path:

The default is valid for the current path and its descendants path. (Path is not the local disk path of the file where the code resides, but the path relationship of the URL request)
Cookies with the same name under different paths can be stored in the browser at the same time, the browser will first look up the current directory of valid cookies, and then look up, all valid will be carried to the server, the server side when the formation of $_cookie, there will be rewrite effect, the first occurrence of the reservation,
The site root directory can be valid by setting the Setcookie () fourth argument to '/', that is, the whole station is valid. The valid path is determined by the browser and the server notifies the browser when the valid path is set

Setcookie (key,val,0, '/');

Valid fields:

The default cookie is valid only under the current domain

You can extend to all subdomains under a domain name by setting a valid domain for the cookie

Me.com      //First class domain
lig.me.com    //Level two domain
bee.me.com    //Level two domain name
setcookie (key,val,0, ', ' me.com ');

is secure transport only

The default is to bring a valid cookie to the server regardless of the HTTP and HTTPS protocol that the browser is emitting

You can set the sixth argument to true to activate a secure transport only, when the browser sends the request if it is an HTTP protocol, it will not send these cookie,apache that are set to only secure transmission need to load the OpenSSL module to use the HTTPS protocol

Setcookie (key,val,0, ",", true);

HttpOnly

Scripts stored by the default browser can be processed by other script calls
Only cookies can be used in HTTP requests by setting the 7th argument

Setcookie (key,val,0, ",", false,true);

Attention:

Cookie value can only be string type
The cookie key can be written in the form of an array subscript
No output (output_buffering) before Setcookie ()

More interested in PHP related content readers can view the site topics: "PHP Cookie Usage Summary", "PHP Array" operation Techniques Daquan, "PHP Basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introduction Tutorial" , "PHP Network Programming Skills Summary", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.