PHP session-processing function session, conversational function session_php Tutorial

Source: Internet
Author: User
Tags php session unique id

Session handler function session in PHP


PHP Session variables

When you run an application, you open it, make some changes, and then close it. It's like a conversation. The computer knows who you are. It knows when you start the application and when it terminates. But on the internet, there is a problem: the server does not know who you are and what you do, because the HTTP address does not maintain state.

By storing user information on the server for later use, the PHP session solves the problem (such as user name, purchase of goods, etc.). However, the session information is temporary and will be deleted when the user leaves the site. If you need to store information permanently, you can store the data in a database.

The Session works by creating a unique ID (UID) for each visitor and storing the variables based on this UID. The UID is stored in a cookie or transmitted through a URL.

1. Start a session

Before you store the information in the session, start the conversation. PHP provides the session_start () function to start or continue a session. Defined:

1 bool session_start(void)

The call is as follows:

1 
  
    php session_start (); ?> 2 < HTML > 3 < Body 
   > 
     Body > 4 
    
      html>  

Attention:

(1) the session_start () function must precede the label , that is, the function must be called before any output, often when the program is written without paying attention to enter a space or a carriage return, this will be an error. Particular attention should be paid to this point. (I've been in a hole)

(2) thesession_start () function returns true whether or not a session has been successfully created, so using any exception handling will not work .

(3) You can also enable the configuration Directive Session.auto_start, which eliminates the need to execute this function, but in this case each PHP page will start or continue a session when it executes.

2. storing or reading sessions

The correct way to store and read session variables is to use PHP's $_session variable. $_session is a global parameter provided by PHP and is designed to store and read the session. (Note that the key name of the associative array is consistent with the naming rules for normal variables)

When you store a session, you can assign a value directly to it.

1 $_session[' season '] = ' fall ';

The above set a session element with a key named "season" with a value of "Autumn". When read, it is like calling a normal array element.

The following two-segment code shows how to store and read a session element.

This is the session1.php file:

  1 !--? 
  
    php 
    2  
   /*  
    *  
    3  
    * Created by Phpstorm.  
    4  
    * User:yuxiu  
    5  
    * DATE:2016/5/26  
    6  
    * time:1 4:11  
    7  
    */ 
    8  
    if  (
    isset  (
    $_post  ['                                Submit ' 
   ]) { 
    9  
    session_start  (); 
   // 
    start building a session        
    ten  
    $_session  [' season '] = 
    $_post  [' Season ']; 
   // 
    Store session Data             
    one  
    header  ("Location:session2.php"); 
   // 
    special attention should be given to formatting problems in the header ()  
     
     
    +  
   }  
    all ? 
    +  
    Storage session  
     
    
    ( 
   ) Select the data you want to set:  
    +  
  

This is the session2.php file:

1 
 Php2 /**3 * Created by Phpstorm.4 * User:yuxiu5 * DATE:2016/5/266 * Time:14:137  */8 Session_Start();//establish or continue a session9 $season=$_session[' Season '];//Reading session DataTen  One Echo"Read Session

"; A Switch($season) { - CaseSpring; - Echo' Now is the green Spring! '; the Break; - CaseSummer; - Echo' Now is the summer of passion! '; - Break; + CaseAutumn; - Echo' Now is the autumn of the harvest fruit! '; + Break; A CaseWinter; at Echo' It's snowy winter! '; - Break; - default ; - Echo' Sorry, there is no data in the session or there is no such conversation! '; - } -?>

In session1.php, you first create a session with Session_Start (), then store the submitted season data using array assignments, and finally jump directly to the beginning using the header () function. In the session2.php file, it is also necessary for the session_start () function to continue a session and invoke the session information using a session array.

3. Destroying sessions

When the session is no longer in use, it is necessary to artificially destroy it, although PHP has the ability to automatically destroy the session, but this will make the program less efficient. You can use the unset () function or the Session_destroy () function.

 
  unset ($_session[' season ']);? >

Or:

 
  Session_destroy ();     Note that using this function resets the session array, which loses all of the stored session data ?>

http://www.bkjia.com/PHPjc/1131622.html www.bkjia.com true http://www.bkjia.com/PHPjc/1131622.html techarticle Session handler for PHP sessions, session function sessions PHP session variables when you run an application, you open it, make some changes, and then close it. It's like a conversation. ...

  • Related Article

    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.