Php session application details, session details

Source: Internet
Author: User
Tags php session temporary file storage

Php session application details, session details

Php session advanced application

Session is very important in web technology. Because a web page is a stateless Connection Program, you cannot know the browsing status of the user. Through session, you can record the user's information for the user to confirm when the user submits Requirements for the web server in this identity again.

For example, if a user does not have a session when Browsing an e-commerce website, the user needs to enter the account password for each browsing.

1. Session temporary file

On the server, if you save all your sessions to a temporary directory, the security and efficiency of the server will be reduced. Opening the server storage site will be very slow.

Using the session_save_path () function of PHP to store Session temporary files can reduce server efficiency and slow site opening caused by temporary file storage.

The sample code is as follows:

<? Php $ path = ". /tmp/"; // set the session storage path session_save_path ($ path); session_start (); $ _ SESSION ['username'] = true;?>

Note:

Session_save_path () must be executed before session_start.

2. Session cache

Session cache temporarily stores content on the webpage to the Temporary INternet Files folder on the IE client, and you can set the cache time.

The Session cache uses the session_cache_limiter () function. Its syntax is as follows:

string session_cache_limiter([string cache_limiter]); 

The cache_limiter parameter is public or private. The session of a colleague is not on the server, but on the client. Not displayed on the server.

The cache time settings use the session_cache_expire () function syntax as follows:

int session_cache_expire([int new_cahche_expire]); 

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

Note:

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

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

<? Phpsession_cache_limiter ("private"); $ cache_limit = session_cache_limiter (); // enable the Client Cache echo "the cache limit is :". $ cache_limit. "\ n"; session_cache_expire (30); $ cache_expire = session_cache_expire (); // set the Client Cache Time echo "the client cache time is :". $ cache_expire. "minute \ n"; session_start ();?>

The running result is as follows:

3. Session database storage

In php, Session database storage is mainly implemented through the session_set_save_handler () function. The syntax is as follows:
Bool session_set_save_handler (string open, string close, string read, string write, string destroy, string gc );


Next we will separately package these six parameters (functions). After Learning Object-oriented programming, we will have a clearer understanding.

(1) encapsulate the session_open () function. The Code is as follows:

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. The Code is as follows:

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

(3) encapsulate the session_read () function, set the UNIX timestamp of the current time in the function, and find the Session business card and content based on $ key. The Code is as follows:

Function _ session_read ($ key) {golbal $ handle; // global variable $ handle connects to the database $ time = time (); // set the 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) encapsulate the session_write () function. The function sets the Session expiration time and finds the Session name and content. If the query result is null. Insert the Session on the page to the Database Based on session_id, session_name, and expiration time. If the query result is not blank, modify the Session storage information in the database based on $ key. The Code is as follows:

Function _ session_write ($ key, $ data) {global $ handle; $ time = 60*60; $ lapse_time = time () + $ time; // obtain the 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) encapsulate session_destroy () and delete 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) encapsulate session_gc () and delete expired sessions based on the Session expiration time. 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);}

The specific code will not be executed. I will show it to you when I finish learning object-oriented programming.

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.