PHP session usage and Sessions entry instance application

Source: Internet
Author: User
Tags md5 session id php session php tutorial sessions mysql database


The session header message has been sent for the same reason as a cookie.
In PHP Tutorial 5, the registry configuration options for all PHP sessions are programmable, and in general, we do not have to modify their configuration. To learn about PHP's session registry configuration options, refer to the Manual sessions processing function.
Session to save the data, is serialized $_session array to store, so there are serialization of the problem, there may be special characters to use the Base64_encode function code, read the time to use Base64_decode decoding


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

<?php
Session_Start (); Start up your PHP session!
?>

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

Store Session Variables
When you want to store the session user data using the $ _session associative array. This is your two storage and retrieval session data. There are other ways to perform this storage operation in previous versions of PHP, but it has been updated, which is the right way to do it.

<?php
Session_Start ();
$_session[' views '] = 1; Store session Data
echo "pageviews =". $_session[' views ']; Retrieve data
?>
See a simple shopping cart instance

<?php
Session_Start ();
if (isset ($_session[' views '))
$_session[' views '] = $_session[' views ']+ 1;
Else
$_session[' views '] = 1;

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

<?php
Session_Start ();
if (isset ($_session[' cart '))
unset ($_session[' cart '));
?>

<?php
Session_Start ();
Session_destroy ();
?>


Session Use instance

<?php
/**
* Validity of the session
*
*/
function Sessionverify () {
if (!isset ($_session[' user_agent ')) {
$_session[' user_agent '] = MD5 ($_server[' remote_addr ')
. $_server[' http_user_agent ']);
}
/* Reassign session ID if user session ID is forged * *
ElseIf ($_session[' user_agent ']!= MD5 ($_server[' remote_addr ')
. $_server[' Http_user_agent ']) {
SESSION_REGENERATE_ID ();
}
}

/**
* Destroy session
* Three-step perfect implementation, not leaking
*
*/
function Sessiondestroy () {
Session_destroy ();
Setcookie (Session_name (), ", Time ()-3600);
$_session = Array ();
}
?>


Session solves the problem of user information that PHP allows you to store for later use (that is, the user name, items in the shopping cart, etc.). However, this session of the information is temporary, usually will be deleted, soon after the user has left the site, it uses sessions.

It is important to think about if the session's temporary storage is applicable to your site. If you need a longer term store, you need to find another solution like a MySQL database tutorial.

Session work creates a unique identification code (UID) for each visitor, and stores variables on this base ID. This helps prevent two of users from gaining access to the same Web page with another chaotic time.

Note: If you are not in session programming experience, it is not recommended that you use on the site, need a high security session, because there are security vulnerabilities that require some advanced technology to jam.

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

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.