Easily implement the SESSION's MySQL processing

zhaozj2021-02-11  203

Typically, session is saved in a temporary file, but if you save it in the database, you will bring us a lot of benefits, such as statistics. Do not say nonsense, see the program:

Function open ($ save_path, $ session_name) {Global $ DB, $ Remote_addr; $ db-> query ("delete from sessions where sessionlast query "Select * from sessions where sessionid = '". Session_id (). "') && $ arry = $ db-> fetch_array ()) $ db-> query (" Update sessions set sessionlast = now () wheresessionID = '" .Session_ID (). "'); Else $ db-> query (" INSERT INTO sessions setsessionid ='. session_id (). ", sessionname = '$ transote_addr', sessionLast = 'now ()'); Return (True);

Function close ()

Return True;}

Function Read ($ ID) {Global $ db; if (! $ db-> query ("SELECT SESSIONID from> Query (" SELECT SESSIONID AD ') || $ dB-> Num_ROWS () <= 0) Return False; $ SQL = "Select sessionData from sessions where sessionid = '$ ID'"; $ db-> query ($ sql); list ($ sess_data) = $ dB-> fetch_row (); return ($ sess_data);}

Function Write ($ ID, $ sess_data) {global $ db; if (! $ db-> query ("Select sessionid from sessions where sessionID = '$ ID') || $ db-> num_rows () <= 0) Return false; if ($ db-> query ("Update sessions setsessionData = '$ sess_data', sessionLast = now () where sessionId = '$ ID'") Return True; Else Return False;}

Function Destroy ($ ID) {Global $ DB; $ DB-> Query ("Delete from sessions where sessionid = '$ ID'");}

Function GC ($ Maxlifetime) {Return True;}

Session_set_save_handler ("Open", "Close", "Read", "WRITE", "Destroy", "GC"); session_start ();

?> Note: Data table: sessionscreate table sessions (sessionid varchar (50) Not null, sessionname varchar (50) Not null, sessionData blob, sessionlast datetime default '0000-00-00 00:00:00' Not Null, Primary Key SESSIONID);

转载请注明原文地址:https://www.9cbs.com/read-5438.html

New Post(0)