PHP Session Usage and Sessions Into Example Application_PHP Tutorial

WBOY
Release: 2016-07-13 10:58:32
Original
874 people have browsed it

PHP Session usage and Sessions into instance application


The reason why the session header information has been sent is the same as that of cookies.
In PHP Tutorial 5, all PHP session registry configuration options are configurable during programming. Generally, we do not need to modify their configuration. To learn about PHP session registry configuration options, please refer to the Session in the manual. Processing function.
When the session saves data, it is stored through the serialized $_SESSION array, so there are problems with serialization. There may be special character values ​​that need to be encoded with the base64_encode function, and then decoded with base64_decode when reading


Below is a simple script that you should start a PHP session at the beginning of your PHP code.

session_start(); // start up your PHP session!
?>

This small piece of code will register a user's session with the server, allowing you to start saving user information and assigning a UID (unique identification number to that user's session).

Storing session variables
When you want to store user data in session use the $_SESSION associative array. This is where you two store and retrieve session data. There were other ways to perform this storage operation in previous PHP versions, but it has been updated and this is the correct way to do it.

session_start();
$_SESSION['views'] = 1; // store session data
echo "Pageviews = ". $_SESSION['views']; //retrieve data
?>
Take a look at a simple shopping cart example

session_start();
if(isset($_SESSION['views']))
$_SESSION['views'] = $_SESSION['views']+ 1;
else
$_SESSION['views'] = 1;

echo "views = ". $_SESSION['views'];
?>

session_start();
if(isset($_SESSION['cart']))
Unset($_SESSION['cart']);
?>

session_start();
session_destroy();
?>


Session usage examples

/**
* Verify the legitimacy of the session
*
​*/
function sessionVerify() {
If(!isset($_SESSION['user_agent'])){
         $_SESSION['user_agent'] = MD5($_SERVER['REMOTE_ADDR']
.$_SERVER['HTTP_USER_AGENT']);
}
/* If the user session ID is forged, reassign the session ID */
elseif ($_SESSION['user_agent'] != MD5($_SERVER['REMOTE_ADDR']
. $_SERVER['HTTP_USER_AGENT'])) {
session_regenerate_id();
}
}

/**
* Destroy session
* Perfect implementation in three steps, don’t miss it
*
​*/
function sessionDestroy() {
Session_destroy();
setcookie(session_name(),'',time()-3600);
$_SESSION = array();
}
?>


Sessions solve the problem of PHP allowing you to store user information on the server for later use (i.e. username, items in shopping cart, etc.). However, this session information is temporary and will usually be deleted soon after the user has left the website, which uses the session.

It is important to think about if temporary storage of sessions is appropriate for your website. If you need a longer term storage, you'll need to find another solution, like a MySQL database tutorial.

Session works by creating a unique identification number (UID) for each visitor and storing variables based on this ID. This helps prevent two users from getting their data confused with another when visiting the same web page.

Note: If you are not experienced with session programming, it is not recommended that you use it on websites that require highly secure sessions, because there are security holes that require some advanced technology to block.

Start a PHP session
Before you can start storing user information in your PHP session, you must first start the session. When you start a session, it must be at the beginning of your code, before any HTML or text is sent.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632000.htmlTechArticlePHP Session usage and Sessions into the instance application session. The reason why the header information has been sent is the same as the cookie. In PHP Tutorial 5 In, all the registry configuration options of php session are programmable...
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