Understanding session Control in PHP

Source: Internet
Author: User
Tags learn php session id php session

If you have not previously contacted the station or network programming, just learn PHP from the beginning, and use PHP to build dynamic site, then the session (sessions) for beginners is a bit difficult to understand. So what exactly is a conversation? Understanding a concept requires starting with the background or problem it produces, so go back to the Web environment where it is located and the HTTP protocol it uses.

HTTP is a stateless protocol, which means that the HTTP protocol does not have a built-in mechanism to maintain state between two transactions. When a user requests a page and then requests another page, HTTP will not be able to tell us that both requests are from the same user. From this we will feel very strange, usually we in the forum to visit the post or e-commerce website shopping, as long as we are in this site, no matter how we jump, from one page to another page, the site will always remember who I am, such as tell you buy what things. This is how to do it, it is estimated that the use of the session control mentioned in this article.

The idea of session control is to be able to track users in a Web site based on a session. So how is PHP's session implemented?

The session ID of PHP is driven by a unique session ID, which is an encrypted random number that is generated by PHP and saved to the client during the lifetime of the session. We know that the client (ie, the browser) keeps the data in a cookie, so the session ID of PHP is generally stored in the cookie of the user's machine. After understanding the cookie we know that the browser can disable cookies, so that the session function is not invalid? So there is also a pattern for PHP session control, which is to pass the session ID in the URL. If we look at the site with a little attention, some URLs have a string that looks like random numbers, then it is very likely that the URL of this form of session control.

In this case, some people may have doubts that the client simply saves a session ID, so the session variables that are stored in the session control, such as the list of items you buy when you shop, are stored in which place? Obviously, session variables are used on the server side, so these session variables must be stored on the server side. By default, session variables are stored in the server's normal file (you can also configure your own database to save, you can Google), the role of the session ID is like a key, in the server side of the file to save the session to find the session ID corresponding to the session variable, such as the purchase of a list of items.

Then the whole process of session control might look like this, when a user logs on or browses a site's page for the first time, the site generates a PHP session ID and sends it to the client (browser) via a cookie. When the user clicks on another page of the site, the browser starts to connect to the URL. Before connecting, the browser searches for locally saved cookies and submits them to the server if there are any cookies in the cookie that are related to the URL being connected. In the case of a login or first connection, a cookie associated with the site URL (the saved session ID) has been generated, so when the user connects to the site again, the site can identify the user from the session ID and remove the session variable associated with the session ID from the server's session file. Thus maintaining continuity between transactions.

The following is a practical introduction to PHP's conversational capabilities.

The basic steps for using a session are as follows:

1) Start a session

Call the Session_Start () function, and the function's specific functions can be consulted in the PHP documentation. It is important to note that this function must be called at the beginning of the script that uses the session, and if not, all information saved in that session cannot be used in the script. In addition to manually calling the Session_Start () function, it is also possible to automatically configure PHP automatic calls, which can be used by Google.

2) Register a session variable

After PHP4.1, the session variable is saved in the Super Global array $_session. To create a session variable, simply set an element in the array, such as $_session[' myvar ' = 5;

3) Use a Session variable

To use a session variable is simple, use the $_session array to access the saved session variables, such as Echo $_session[' Mywar '; will print out 5. You must first start a session with the Session_Start () function before using a session.

4) Unregister variables and destroy sessions

Unregister variables directly using unset, such as unset ($_session[' myvar '), how to destroy all session variables at once, you can use unset ($_session); When a session is finished, you should first unregister all variables and then call Session_destroy () to clear the session ID.

Next, we end this article with a simple conversation example. In this example, we have implemented 3 pages.

<? php//page1.php start a session and register a variable session_start();  $_session[' sess_var ' = "Hello, world!" ; $_session[' Sess_var '. ' <br/> ';? ><a href= "page2.php" >Next page</a>         
<? php//page2.php access a session and unregister it session_start();  $_session[' sess_var ' = "Hello, world!" ; $_session[' Sess_var '. ' <br/> ';  unset ($_session[' Sess_var ');? ><a href= "page3.php" >Next page</a>          
<? php//page3.php End Session Session_Start();  $_session[' Sess_var '. ' <br/> ';  Session_destroy();? >        

Then visit page1.php and click on the next Page sequentially, you can see the output in the browser Page1 and Page2 can be seen $_session[' Sess_var ' value, Page3 is empty.

With this example, we have a better understanding of how the session is used in PHP.

Understanding session Control in PHP

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.