PHPSESSION literacy. In this article, you can know how to use the basics of session. After reading this article, you may have learned the basic usage of the session! PHPSession variables when you run a program in this article, you can know how to use the basics of session. After reading this article, you may have learned the basic usage of the session!
PHP Session variable
When you run an application, you open it, make some changes, and then close it. This is like a session. The computer knows who you are. It knows when to start the application and when to terminate it. However, there is a problem on the Internet: the server does not know who you are and what you are doing, because the HTTP address cannot be maintained.
By storing user information on the server for subsequent use, PHP session solves this problem (such as user name and product purchase ). However, the session information is temporary and will be deleted after the user leaves the website. If you need to store information permanently, you can store the data in the database.
The Session mechanism is to create a unique id (UID) for each visitor and store the variables based on the UID. The UID is stored in the cookie or transmitted through the URL (PS. In most cases, we use cookies to save it ).
Start PHP Session
When a php page is running, the default session is not started. we need to start it manually. This is easy because you only need to call the following functions!
Session_start ()
However, this function should be called when html code is not output. this is worth noting! Otherwise, an error is reported! Some people are wondering why they obviously saved a value in the session and disappeared on another page! It is very likely that the session_start () function is not called.
Store Session variables
The session in PHP is easy to use. it is actually an array variable $ _ SESSION. You can use the following statement to store session variables:
$ _ SESSION ['username'] = 'www .zeroplace.cn ';
On any other page, you can use or modify this variable at any time!
End Session
If we store basic user information in the session during user logon, we may want to destroy the session when the user exits.
Unset ($ _ SESSION ['username']);
You can call the following function to clear all session variables.
Session_destroy ();
Copyright of the 0th space
Bytes. After reading this article, you may have learned the basic usage of the session! PHP Session variable when you run a response...