Home  >  Article  >  Backend Development  >  Talk about PHP syntax (4)_PHP tutorial

Talk about PHP syntax (4)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:06:32808browse

Text:
Cookie and Session are mentioned at the end of the above article ("Talk about PHP Grammar (3)". This article will give some introduction to these two technologies.
We often pronounce Cookie as "Kuji" , also called "little dessert". It is a small file stored in the client's browser. It was developed to solve the problem of HTTP connection without memory and can be used to track users or check reloading. Return the user to confirm. PHP provides the setcookie() function to set Cookies. Therefore, the setcookie() function must be called before the web page data is sent to the browser. It is the same as calling the header() function.
Cookie must be provided by the host; therefore, we must send a header with a cookie set in the CGI program. The following is the setcookie() function in PHP to set the cookie. Example:
setcookie("user","wind",time()+3600,"/php/","http://www.oso.com.cn") ;
?>
Among them, user is the name of the cookie; wind is the value of the cookie; time()+3600 is the validity time of the cookie; /php/ is the relevant path of the cookie; http: //www.kunoso.com.cn is the website for this 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 this requires some understanding of the HTTP header information, so the author does not recommend using this method. It is more convenient to use setcookie().
When reading cookies, the browser is connecting When accessing a certain website, it will automatically check whether there is a cookie for that website, and if so, it will be automatically passed to the server. In PHP, the returned cookie will be returned as a variable, such as the cookie set above. After that, a $user variable will be formed, whose value is wind.
However, cookie has a fatal disadvantage, that is, if the customer turns off cookie reception and cannot store the cookie to the client, all operations will go wrong. Session is provided in PHP4 instead of Cookie.
The biggest difference between Session and Cookie is that Cookie stores information on the client side, while Session stores it on the server side. In fact, Session provides a global view for PHP scripts. Kun variable. The example is as follows:
Set up a Session named user with the value wind
session_start();
$user="wind";
session_register("user") ;
?>
Read Session and the result is "Welcome! wind"
session_start();
echo "Welcome! $user";
?>
This article only briefly discusses Cookie and Session technology. For those who want to know more about this technology, please refer to other books.
--(to be continued)--

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315392.htmlTechArticleText: Cookie and Session were mentioned at the end of the above article ("Talk about PHP Grammar (3)", this article is about Let’s give some introduction to these two technologies. We often pronounce Cookie as “Kuji”, also call it “small...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn