Implement the SESSION function in PHP3

Source: Internet
Author: User
SESSION function library: session. inc. php3? Phpif (! Isset ($ __session_inc _) {$ __session_inc _ 1; // require (cookie. inc. php3); # reset SESSION function library: session. inc. php3

If (! Isset ($ __session_inc __)){
$ __Session_inc __= 1;
// Require ("cookie. inc. php3 ");
#-------------------------------------------------------------------
# Session Management v1.0 21.6.1998
# (C) Wild Karl Heinz
#
# This Include handle Session based variable handling
#
# Please feel free and use it. If you make it more functional
# It wocould be nice to send me a copy.
#
# Don't forget-Mysql_connect!
#
# The database structure
# Table structure for table 'session'
#
# Create table session (
# Id int (11) DEFAULT '0' not null auto_increment,
# Sid varchar (20) DEFAULT ''not null,
# Val blob,
# Times timestamp (14 ),
# Primary key (id ),
# KEY sid (sid ),
# UNIQUE sid_2 (sid)
#);
#
# You'll miss here a cron job to delete the old sessions from db
#-------------------------------------------------------------------

// Note the 'create table' statement commented out above,
// You need to execute this statement on the database you are using,
// The table name may not be a session, so you need to set the $ sess_table variable below.

// Set the database name and table name here.
// However, we recommend that you use session as the table name.
$ Sess_db = 'dbname ';
$ Sess_table = 'session ';

#----------------------------------------------------
# Session_CheckID-check, set, and return Session-ID
# Parameter ......: cookie retention time (in minutes)
# This cookie is valid only for the current session.
# This is actually the same as the SESSION validity period in ASP.
# Return value...: a unique Session-ID (stored as a cookie)
#----------------------------------------------------
Function Session_CheckID ($ min)
{
Global $ sess_sid;

If (! $ Sess_sid ){
$ Sess_sid = uniqid (SC); // Obtain a unique random number.
/*
If ($ min> 0 ){
SetCookie ("sess_sid", $ sess_sid, time () ($ min * 60), "/", "", 0 );
}
Else {
SetCookie ("sess_sid", $ sess_sid, "", "/", "", 0 );
}
The above is the original code, which may cause errors. Therefore, a better function is used.
Function library: cookie. inc. php3
*/
Jssetcookie ("sess_sid", $ sess_sid, $ min );
Return (false );
}
Else {
Return (true );
}
}

#----------------------------------------------------------
# Str2arr-converts a string to a session array
# Parameter ......: string
# Return value...: Global Array (actually session)
# Purpose of this function: convert a string to a session array
# For example, "session [username] = yourid & session [userpass] = 12345"
# Will be converted to the following array
# Session [username] = "yourid"
# Session [userpass] = "12345"
# Note the usage of functions split (), each (), list (), and eval.
#----------------------------------------------------------
Function str2arr ($ ts)
{
Global $ session;

$ Vals = split ("&", $ ts );
While (list ($ key, $ val) = each ($ vals )){
List ($ name, $ wert) = split ("=", $ val );
If ($ val) eval ("$ name =" $ wert ";");
}
}

#----------------------------------------------------------
# Session_read ()-fetch data from the SESSION table and convert it to the session array
# Parameter ......: None
# Return value ......: if data is read, true is returned; otherwise, false is returned.
# Note ......: the str2arr () function is used.
#----------------------------------------------------------
Function session_read ()
{
# Hash array to keep session-variables
Global $ session;
Global $ sess_sid, $ sess_db, $ sess_table, $ sess_error;

$ Sel = "Select val from $ sess_table where sid = '$ sess_sid '";
$ Res = mysql_db_query ($ sess_db, $ sel );
If (mysql_numrows ($ res )){
$ Val = mysql_result ($ res, 0, "val ");
Str2arr ($ val );
Mysql_free_result ($ res );
Return (true );
}
Else {
Return (false );
$ Sess_error = mysql_error ();
}
}

#------------------------------------------------------
# Split_Array ()-converts a session array to a string
# Parameter ......: Array
# Return value...: string obtained from array conversion
#
# Thanks to Rasmus (this person seems to be the inventor of PHP)
# Note: convert the session array to a string
# Session [username] = "yourid"
# Session [userpass] = "12345"
# It will be converted to "session [username] = yourid & session [userpass] = 12345"
# At the same time, this function considers that an element of the array is also data.
# This function is designed as a recursive function.
#------------------------------------------------------
Function Split_Array ($ arr, $ a = "", $ B = "", $ c = "")
{
While (list ($ key, $ val) = each ($ arr )){
If (is_array ($ val )){
$ Ts. = Split_Array ($ arr [$ key],
(Strlen ($ )? $ A: $ key ),
(Strlen ($ B )? $ B: (strlen ($ )? $ Key :"")),
(Strlen ($ c )? $ C: (strlen ($ B )? $ Key :"")));
}
Else {
$ Ts. = "session ";
$ Ts. = $? "[$ A]": "";
$ Ts. = $ B? "[$ B]": "";
$ Ts. = $ c? "[$ C]": "";
$ Ts. = "[$ key] = $ val &";
}
}
Return ($ ts );
}

#---------------------------------------------------
# Session_write-convert the session array to a string and store it in the session table.
# Parameter.: None
# Return value...: true is returned if the data is stored normally; otherwise, false is returned.
#---------------------------------------------------
Function session_write ()
{
# Hash array to keep session-variables
Global $ session;

Global $ sess_sid, $ sess_db, $ sess_table;
Global $ sess_error;

# If you like to delete a session-cookie
# You must check it before writting the session
# Array

If (! $ Sess_sid) {session_checkid (0 );}

$ Ts = Split_Array ($ session );
If ($ ts> "") {$ ts = substr ($ ts, 0, strlen ($ ts)-1 );}
$ Res = mysql_db_query ($ sess_db, "Select * from session where sid = '$ sess_s '");
If (mysql_numrows ($ res) = 0 ){
$ Sel = "Insert into $ sess_table (id, sid, val, times )";
$ Sel. = "values (0, '$ sess_sid', '$ ts', NULL )";
}
Else {
$ Sel = "Update $ sess_table set val = '$ ts ',";
$ Sel. = "times = NULL where sid = '$ sess_sid '";
}
If (! Mysql_db_query ($ sess_db, $ sel )){
$ Sess_error = mysql_error ();
Return (false );
}
Else {return (true );}
}

#---------------------------------------------
# Session_del-clear all current sessions
# Delete the records related to the current session in the session table
# Parameter ......: a random session id
# Return value...: None
#---------------------------------------------
Function session_del ()
{
Global $ session, $ sess_db, $ sess_table, $ sess_sid;

$ Sel = "Delete from $ sess_table where sid = '$ sess_sid '";
If (! Mysql_db_query ($ sess_db, $ sel )){
$ Sess_error = mysql_error ();
}
$ Sess_sid = '';
}
}
?>

Original Author: unknown

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.