How to use the session in PHP

Source: Internet
Author: User

The session in PHP is by default a cookie used by the client. When the client's cookie is disabled, it is automatically passed through the query_string.

There are 11 functions in the PHP processing session, and we'll talk about several functions we'll use.

1, Session_Start

function function: Start a session or return a session that already exists.

Function prototype: Boolean session_start (void);

Return Value: Boolean value

Session working principle

First, PHP creates a unique string for the user who creates the session, which is used to mark the session of the user. This string is generally referred to as the session ID. Then the "Sess" +session ID is the filename (for example, a session ID of 111, Then the file name is sess_111 to create a file in the server's filesystem that holds the variable names and values of the global variables defined by the user in the session. The session ID is then saved in the client's file system as a cookie named Phpsession.
Then, when the user connects to the server again to access a PHP script, PHP obtains the session Id of the user's session from the Peesession cookie that the user sends, and according to the session A file in which the ID holds session information from the server's file system. Finally, the value of the global variable set by the user at the last connection is read from this file.
So we can see that the session works the same way as the identity authentication we described in the previous section. The difference is only session to save the information in the server's file system, and we keep the information in the database. Of course, using the session benefits is the data to save and access is automatically completed by PHP, and the direct use of cookies will need to do their own data preservation and acquisition.
The session takes advantage of the cookie's identity flag function to store the information that the user needs to save on the server when browsing the site. This session not only overcomes the HTTP protocol flaw, but also prevents the information leakage, and facilitates the programmer's use, is a very good solution. However, the function of the session only PHP4 support, PHP3 is not support session. As a result, readers using PHP3 to build a Web site can only use cookies directly.

Session instance


In the session, first of all, PHP in the php.ini file in the Session.sava.path set.

The code is as follows Copy Code

<?php
Start session
Session_Start ();
Declares a variable named admin and assigns null values.
$_session["admin" = null;
?>
<?php
After form submission ...
$posts = $_post;
Clear some whitespace symbols
foreach ($posts as $key => $value)
{$posts [$key] = Trim ($value); }
$password = MD5 ($posts ["Password"]);
$username = $posts ["username"];
$query = "Select ' username ' from ' user ' WHERE ' password ' = ' $password '";
Get query Results
$userInfo = $DB->getrow ($query);
if (!empty ($userInfo))
{
if ($userInfo ["username"] = = $username)
{//when validation passes, start session
Session_Start ();
Register login successfully admin variable and assign a value of True
$_session["Admin"] = true;
}
Else
{
Die ("Username password error");
}
}
Else
{
Die ("Username password error");
}
?>
We start the session on a page that requires user authentication to determine whether to log in:
<?php
Prevent global variables from causing security risks
$admin = false;
Start a session, this step is essential
Session_Start ();
Judge whether to log in
if (Isset ($_session["admin")) && $_session["admin"] = = True)
{
echo "You have successfully landed";
}
Else
{//validation failed, set $_session[' admin ' to false
$_session["admin"] = false;
Die ("You are not entitled to access");
}
?>
<?php session_start ();
Save one day
$lifeTime = 24 * 3600;
Setcookie (Session_name (), session_id (), time () + $lifeTime, "/");
?>
<?php session_start ();
This approach is to destroy a variable that was originally registered
unset ($_session["admin"));
This method is to destroy the entire session file
Session_destroy ();
?>
Let's set the lifetime of the session manually:
<?php session_start ();
Save one day
$lifeTime = 24 * 3600;
Setcookie (Session_name (), session_id (), time () + $lifeTime, "/");
?>
In fact, the session also provides a function session_set_cookie_params (); To set the lifetime of the session, the function must be called before the session_start () function call:
<?php
Save one day
$lifeTime = 24 * 3600;
Session_set_cookie_params ($lifeTime);
Session_Start ();
$_session["Admin"] = true;
?>

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.