Phpsession application code example

Source: Internet
Author: User
Tags temporary file storage
This article mainly introduces phpsession application details. For more information about php session applications, see the following article, for more information, see

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:

 

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:

 

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 above is a detailed description of the php session application code instance. For more information, see other related articles in the first PHP community!

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.