Home > Backend Development > PHP Tutorial > Chapter 12 Session Control_PHP Tutorial

Chapter 12 Session Control_PHP Tutorial

WBOY
Release: 2016-07-13 10:31:58
Original
884 people have browsed it

Learning points:
1.Cookie application
2.Session session processing

HTTP (Hypertext Transfer Protocol) defines all the rules for transmitting text, graphics, video and all
other data over the World Wide Web (WWW). HTTP is a stateless protocol, meaning that the processing of each request has nothing to do with previous or subsequent
requests. While this simplified implementation made a great contribution to the popularity of HTTP, it can be a bit confusing for developers who want to create complex
Web applications. To solve this problem, there is a way to store a small amount of information (cookie) on the client
machine.
Due to cookie size restrictions, quantity and other reasons, developers have proposed another solution: session will be
processed.

1. Application of Cookies

Set cookie: The setcookie() function can generate a cookie file on the client, which can be saved to the
period, name, value, etc.


Create cookie

<?<span php
</span><span setcookie</span>('name','Lee',<span time</span>()+(7*24*60*60<span ));
</span>?>
Copy after login

Parameter 1: cookie name
Parameter 2: cookie value
Parameter 3: cookie expiration time

Read cookies

<?<span php
</span><span echo</span> <span $_COOKIE</span>['name'<span ];
</span>?>
Copy after login

Delete cookies

<?<span php
</span><span setcookie</span>('name',''<span );
</span><span setcookie</span>('name','Lee',<span time</span>()-1<span );
</span>?>
Copy after login

Restrictions on the use of cookies

1. It must be set before the content of the HTML file is output;
2. Different browsers handle cookies inconsistently, and sometimes incorrect results will occur.
3. The restriction is on the client side. The maximum number of cookies that can be created by a browser is 30, and each cannot
exceed 4KB. The total number of cookies that can be set by each WEB site cannot exceed 20.

2. Session handling

When using session session processing, you must start the session and use session_start() to start the session.

Create session and read session

<?<span php
</span><span session_start</span><span ();
</span><span $_SESSION</span>['name'] = 'Lee'<span ;
</span><span echo</span> <span $_SESSION</span>['name'<span ];
</span>?>
Copy after login

Determine whether the session exists

<?<span php
</span><span session_start</span><span ();
</span><span $_SESSION</span>['name'] = 'Lee'<span ;
</span><span if</span> (<span isset</span>(<span $_SESSION</span>['name'<span ])) {
</span><span echo</span> <span $_SESSION</span>['name'<span ];
}
</span>?>
Copy after login

Delete session

<?<span php
</span><span session_start</span><span ();
</span><span $_SESSION</span>['name'] = 'Lee'<span ;
</span><span unset</span>(<span $_SESSION</span>['name'<span ]);
</span><span echo</span> <span $_SESSION</span>['name'<span ];
</span>?>
Copy after login

Destroy all sessions

<?<span php
</span><span session_start</span><span ();
</span><span $_SESSION</span>['name'] = 'Lee'<span ;
</span><span $_SESSION</span>['name2'] = 'Lee'<span ;
</span><span session_destroy</span><span ();
</span><span echo</span> <span $_SESSION</span>['name'<span ];
</span><span echo</span> <span $_SESSION</span>['name2'<span ];
</span>?>
Copy after login

Note: The article comes from Li Yanhui’s PHP video tutorial. This article is for communication only and may not be used for commercial purposes, otherwise you will be responsible for the consequences.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/759627.htmlTechArticleLearning points: 1.Cookie application 2.Session session processing HTTP (Hypertext Transfer Protocol) defines the use of the World Wide Web (WWW) Transmission of text, graphics, videos and all other data All...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template