Body:
In the last section ("Talking About PHP Syntax (3)," the cookie and session are mentioned in the end, this article introduces the two techniques.
Cookies are often read as "Memory", also called "small dessert". It is a small file stored in the client's browser. It is developed to address a single connection to HTTP without Queensland memory, and can be used to track the user or confirm the users who have returned. PHP provides a setcookie () function that allows you to set cookies. Because cookies are part of the Queensland HTTP header. Therefore, the Setcookie () function must be invoked before the Web page data is passed to the browser. This is the same as calling the header () function.
Cookies must be provided by the host side, so we have to send out a header that has a cookie set in the CGI program. The following is a Queensland example of invoking the Setcookie () function setting cookie in PHP:
<?php
Setcookie ("User", "Wind", Time () +3600, "/php/", "http://www.oso.com.cn");
?>
Where user is the name of the cookie, the value for which wind is the cookie, time () +3600 is the valid duration of the cookie,/php/is the relevant path to the cookie, and http://www. Queensland oso.com.cn the Web site for the cookie.
In fact, in addition to using this way to set cookies, we can also use the header () function such as: Header ("Set-cookie:user=wind"), but this to the HTTP head letter Queensland have knowledge of the line, so I do not recommend the use of this method, Or the use of Setcookie () more convenient.
When a cookie is read, the browser automatically checks to see if there is a cookie for the station, and some of the words will be automatically sent to the server, and in PHP, the returned CO Queensland Okie as a variable. If the cookie set above is returned, a $user variable will be formed, with the value of wind.
However, the cookie has a fatal disadvantage, that is, if the customer turned off the cookie to receive, unable to store cookies to the client, all operations will be wrong. As a result, S Queensland ession are provided in PHP4 to replace cookies.
The biggest difference between a session and a cookie is that the cookie saves the information to the client and the session is stored on the server side. In fact, the session is the PHP script provides a global Queensland variable. Examples are as follows:
Set a session, named user, with the value wind
<?php
Session_Start ();
$user = "Wind";
Session_register ("user");
?>
Read the session, show the result as "welcome you!" Wind
<?php
Session_Start ();
echo "Welcome you!" $user ";
?>
This article is just a brief discussion of cookies and session technology, for those who want to understand the technology, please refer to other books.
--(to be continued)--
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.