Application code Example of PHP session detailed introduction

Source: Internet
Author: User
Tags php session temporary file storage
This article mainly introduces the application of PHP session details of the relevant information, the need for friends can refer to the following

PHP Session Advanced Application

Session in Web technology is very important, because the Web page is a stateless connection program, it is not possible to know the user's browsing status. The session allows you to record information about the user so that the user can confirm the request to the Web server again as that identity.

For example: When users browse e-commerce sites, if there is no session, then users need to enter the account password every time they browse.

1,session temp File

In the server, the security and efficiency of the server can be reduced if all sessions of the user are saved to a temporary directory. Opening a server-stored site can be very slow.

Using the PHP function Session_save_path () function to store session temp files can alleviate the problem of server inefficiency and slow site opening due to temporary file storage.

The sample code is as follows:

<?php$path = "./tmp/";    Set the path of Session store Session_save_path ($path); Session_Start (); $_session[' UserName ']=true;? >

Attention

Session_save_path () must be executed before session_start ().

2,session Cache

The Session cache is the temporary INternet files folder that temporarily stores the contents of a Web page in the IE client and can set the cache time.

The session cache uses the Session_cache_limiter () function, which has the following syntax:

String Session_cache_limiter ([string cache_limiter]);

Where the parameter cache_limiter is public or private. Colleague session is not on the server side, but on the client. is not displayed in the server.

The setting of the cache time, using the function Session_cache_expire () syntax is as follows:

int Session_cache_expire ([int new_cahche_expire]);

The parameter new_cahche_expire is the time number of the session cache, in minutes.

Attention:

These two session functions must be executed before the session_start () function

The sample code for the session cache page is as follows:

<?phpsession_cache_limiter ("private"); $cache _limit =session_cache_limiter ();   Turn on client cache echo "Cache limit is:". $cache _limit. " \ n "; Session_cache_expire (+); $cache _expire = Session_cache_expire ();  Set client cache time echo "Client cache time:". $cache _expire. " Minutes \ n "; session_start ();? >

The results of the operation are as follows:

3,session Database Storage

The database storage of the session in PHP is mainly implemented by the Session_set_save_handler () function. The specific syntax is as follows:
BOOL Session_set_save_handler (string open,string close,string read,string write,string destroy,string GC);


The following 6 parameters (functions) are divided together, after learning the object-oriented programming, we will have a clearer understanding.

(1) encapsulate the Session_open () function with the following code:

function _session_open ($save _path, $session _name) {global $handle; $handle =mysql_connect (' localhost ', ' root ', ' root ') Or Die (' Database connection failed! '); mysql_select_db (' Db_database11 ', $handle) or Die (' database does not exist '); return (true);}

(2) encapsulate the Session_close () function with the following code:

function _session_close () {global $handle; Mysql_close ($handle); return (true);}

(3) package Session_read () function, set the current time in the function of the Unix timestamp, according to $key to find the session card and content. The code is as follows:

function _session_read ($key) {Golbal $handle;     Global variable $handle Connection database (=time);     Set Current Time $sql = "Select Session_data from tb_session where Session_key = ' $key ' and session_time> ' $time '"; $result =mysql _query ($ssql, $handle), $row =mysql_fetch_array ($result), if ($row) {return ($row [' session_data ']);} Else{return (FALSE);}}

(4) package session_write () function, function set the session's expiration time, find the name and content of the session, if the query result is empty. The session in the page is inserted into the database according to the session_id,session_name, expiration time. If the query result is not empty, the session stored information in the database is modified according to the $key. The code is as follows:

function _session_write ($key, $data) {global $handle; $time = 60*60; $lapse _time =time () + $time;   Get unix Timestamp $sql = "Select Session_data from tb_session where Session_key = ' $key ' and session_time> $lapse _time"; Result =mysql_query ($sql, $handle); if (Mysql_num_rows ($result) ==0) {  //no result $sql = "INSERT into tb_session values (' $ Key ', ' $data ', $lapse _time) "; $result =mysql_query ($sql, $handle);} else{$sql = "Update tb_session set session_key= ' $key ', session_data = ' $data ', session_time = $lapse _time where Session_key = ' $key '; $result =mysql_query ($sql, $handle);} return ($result);}

(5) package Session_destroy () to remove Sessin from the database according to $key. The code is as follows:

function _session_destroy () {global $handle; $sql = "Delete from tb_session where Session_key = ' $key '"; $result =mysql_ Query ($sql, $handle);}

(6) package session_gc (), delete the expired session according to the expiration time of the session, the sample code is as follows:

functin _session_gc ($expiry _time) {global $handle; $sql = "Delete from tb_session where session_expiry_time< $expiry _time "; $result =mysql_query ($sql, $handle); return ($result);} 

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.