PHP syntax (6)

Source: Internet
Author: User
Cookie and Session are mentioned at the end of PHP syntax (3). This article introduces these two technologies. Cookie is often referred to as a "database" or "small dessert ". It is a small file stored in the client browser. It is to solve a connection of HTTP ?? SyntaxHighlight

Cookie and Session are mentioned at the end of PHP syntax (3). This article introduces these two technologies.
Cookie is often referred to as a "database" or "small dessert ". It is a small file stored in the client browser. It is to solve a connection of HTTP ?? Developed from memory, it can be used to track users or confirm users who return. PHP provides the setcookie () function to set the Cookie. Because Cookies are part of the HTTP header. Therefore, the setcookie () function must be called before webpage data is transmitted to the browser. This is the same as calling the header () function.
The Cookie must be provided by the host. Therefore, a cookie header must be sent in the CGI program.

 

The following example shows how to call the setcookie () function in PHP to set the cookie: Setcookie ("user", "wind", time () + 3600, "/php/", "http://www.oso.com.cn ");
?>

 

 

 

Here, user is the name of the cookie; wind is the value of the cookie; time () + 3600 is the effective time of the cookie;/php/is the relevant path of the cookie; http: // www .?? The website where oso.com.cn is the cookie.
In fact, in addition to setting cookies in this way, we can also use the header () function such as: header ("Set-cookie: user = wind "), but is this an HTTP header ?? So I do not recommend using this method. it is easier to use setcookie.
When the browser accesses a website, it automatically checks whether there is a cookie for the website. If yes, it will automatically pass it to the server. in PHP, the co ?? Okie is used as a variable. For example, after the cookie is returned, a $ user variable is generated and its value is wind.
However, a fatal drawback of cookies is that if the customer closes the cookie reception and fails to store the cookie to the client, all operations will fail. Therefore, the S ?? Ession to replace Cookie.
The biggest difference between Session and Cookie is that Cookie stores information on the client, while Session stores information on the server. In fact, Session provides a global environment for PHP scripts ?? Variable. Example:
Set a Session named user with the value of wind

Session_start ();
$ User = "wind ";
Session_register ("user ");
?>

Read the Session and the result is "Welcome! Wind"
? Br> Session_start ();
Echo "Welcome! $ User ";
?>


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.