Home >Backend Development >PHP Tutorial >Customization of php session processing_PHP tutorial

Customization of php session processing_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:47:37786browse

user-define-session-inc.php file code:

Copy code The code is as follows:

function mysession_open($save_path, $session_name)
{
@mysql_connect("localhost", "root","1981427") //You need to connect to the database server before selecting the database
or die("database Server connection failed");
@mysql_select_db("test") //Select database mydb
or die("Database does not exist or is unavailable");
return true;
}

function mysession_close()
{
return true;
}

function mysession_read($key)
{
@mysql_connect("localhost", "root" ,"1981427") //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("test") //Select the database mydb
or die( "The database does not exist or is unavailable");
$expiry_time = time(); //Get the Session expiration time
//Execute the SQL statement to get the value of the 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") //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("test") // Select database mydb
or die("Database does not exist or is unavailable");
$expiry_time = time() + 1200; //Get the Session expiration time
//Query whether the key value of the Session already exists
$query = @mysql_query("select session_data from mysession "
."where session_key = '$key'")
or die("SQL statement execution failed");
//if not If exists, perform the insertion operation, otherwise perform the update operation
if(mysql_numrows($query) == 0)
{
//Execute the SQL statement to insert the value of the Session
$query = @mysql_query( "insert into mysession values('$key', '$data', $expiry_time)")
or die("SQL statement execution failed");
}
else
{
//Execute SQL statements 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") //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("test" ) //Select database mydb
or die("Database does not exist or is unavailable");
//Execute SQL statement to 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") //You need to connect to the database server before selecting the database
or die("Database server connection failed");
@mysql_select_db("test") //Select Database mydb
or die("Database does not exist or is unavailable");
$expiry_time = time();
//Execute SQL statement to delete Session
$query = @mysql_query("delete from mysession where session_expiry < $expiry_time")
or die("SQL statement execution failed");
return $query;
}

//Set user-defined Session storage
session_set_save_handler('mysession_open',
'mysession_close',
'mysession_read',
'mysession_write',
'mysession_destroy',
'mysession_gc');
?>

Copy code The code is as follows:

include('user-define -session-inc.php'); //File containing session_set_save_handler definition

session_start();
$_SESSION['username'] = "zhuzhao";
$_SESSION['password' ] = "123456";
?>

Copy code The code is as follows:

include('user-define-session-inc.php'); //File containing the definition of session_set_save_handler

session_start();
echo "UserName:".$_SESSION ['username']."
";
echo "PassWord:".$_SESSION['password']."
";
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319895.htmlTechArticleuser-define-session-inc.php file code: Copy the code as follows: ?php function mysession_open($save_path , $session_name) { @mysql_connect("localhost", "root","1981427") //Select number...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn