user-define-session-inc.php File Code:
Copy the Code code as follows:
function Mysession_open ($save _path, $session _name)
{
@mysql_connect ("localhost", "root", "1981427")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is not available");
return true;
}
function Mysession_close ()
{
return true;
}
function Mysession_read ($key)
{
@mysql_connect ("localhost", "root", "1981427")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is not available");
$expiry _time = time (); Get session Expiration Time
Execute SQL statement to get the value of session
$query = @mysql_query ("Select Session_data from MySession"
." where Session_key = ' $key ' and Session_expiry > $expiry _time ")
Or Die ("SQL statement execution failed");
if ($row = mysql_fetch_array ($query))
return $row [' Session_data '];
Else
return false;
}
function Mysession_write ($key, $data)
{
@mysql_connect ("localhost", "root", "1981427")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is not available");
$expiry _time = time () + 1200; Get session Expiration Time
Query whether the key value of session already exists
$query = @mysql_query ("Select Session_data from MySession"
." where Session_key = ' $key ' ")
Or Die ("SQL statement execution failed");
If it does not exist, the insert operation is performed, or the update operation is performed
if (mysql_numrows ($query) = = 0)
{
Execute SQL statement Insert value of Session
$query = @mysql_query ("INSERT into mysession values (' $key ', ' $data ', $expiry _time)")
Or Die ("SQL statement execution failed");
}
Else
{
Execute SQL statement to update the value of Session
$query = @mysql_query ("Update mysession set"
." Session_data = ' $data ', Session_expiry = $expiry _time "
." where Session_key = ' $key ' ")
Or Die ("SQL statement execution failed");
}
return $query;
}
function Mysession_destroy ($key)
{
@mysql_connect ("localhost", "root", "1981427")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is not available");
Execute SQL statement Delete session
$query = @mysql_query ("Delete from mysession where Session_key = ' $key '")
Or Die ("SQL statement execution failed");
return $query;
}
function mysession_gc ($expiry _time)
{
@mysql_connect ("localhost", "root", "1981427")//Select the database before you need to connect to the database server
Or Die ("Database server Connection Failed");
@mysql_select_db ("test")//Select Database MyDB
Or Die ("database does not exist or is not available");
$expiry _time = time ();
Execute SQL statement Delete session
$query = @mysql_query ("Delete from mysession where Session_expiry < $expiry _time")
Or Die ("SQL statement execution failed");
return $query;
}
Set up user-defined session store
Session_set_save_handler (' Mysession_open ',
' Mysession_close ',
' Mysession_read ',
' Mysession_write ',
' Mysession_destroy ',
' mysession_gc ');
?>
Copy the Code code as follows:
Include (' user-define-session-inc.php '); Files that contain session_set_save_handler definitions
Session_Start ();
$_session[' username '] = "Zhuzhao";
$_session[' password '] = "123456";
?>
Copy the Code code as follows:
Include (' user-define-session-inc.php '); Files that contain session_set_save_handler definitions
Session_Start ();
echo "UserName:". $_session[' UserName ']. "
";
echo "PassWord:". $_session[' PassWord ']. "
";
?>
The above describes the PHP session processing of the customization, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.