PHP session con...LOGIN

PHP session control using session in PHP

After understanding the principle of session, let's learn how to use session in PHP.

1. Open the session
First we need to open the session, then the first function to learn is
bool session_start(), this function has no parameters . Use

   session_start();

at the beginning of the php file to enable a new session or reuse an existing session.

2. Add session data
After opening the session, we can use the $_SESSION variable to access information in the subsequent processing. What we need to know is that the $_SESSION variable is an array. When we want to store information into the session, we should write like this:

   $_SESSION['userName'] = 'wang';

3. Read session data
Reading is very simple, just like we use an array, as follows:

   $userName = $_SESSION['userName'];

Of course, $_SESSION['userName'] can also be used. Used in the same way as arrays.
4. Destroy session data
We can use many ways to destroy session data.
a) unset function
We destroy the XXX variables in the session by using something like

   unset($_SESSION['XXX']);

. PS: Please don’t! Please do not! Please do not unset($_SESSION), which will result in the $_SESSION variable being unable to be used later! ! !
b) Assign an empty array to the session variable

   $_SESSION = array();

We said before that the $_SESSOIN variable is an array, so assigning an empty array is equivalent to destroying the value in the $_SESSION variable of the current session.
c) session_destory() function
This function will destroy all data in the current session and end the current session. However, global variables associated with the current session will not be reset, nor will session cookies be reset.

5. Session extension: Where is the default session stored.
There is such a line in the php.ini configuration file session.save_handler = files,
files, which shows that PHP uses file reading and writing by default to save sessions. So which directory is it in? Keep reading. session.save_path = "/tmp",
There is a ; in front of this line, indicating that it is commented, but even so, PHP's default
session is also saved here, in the /tmp directory. Above:

11.PNG

From the picture we can see that it is indeed under this directory, let’s take a look at the contents

document_2015-08-31_55e44c8eb3e27.PNG

My statement to write the session is:

$_SESSION['as'] = 'as';

Interpret it, the first as represents the as in $_SESSION['as'], and the following s represents This is a string type data, 2 represents the number of bytes occupied by this string, and the final value enclosed in double quotes is as.

Next Section
<?php session_start(); // store session data $_SESSION['views']=1; ?> <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['views']; ?> </body> </html>
submitReset Code
ChapterCourseware