PHP USES the mysql database to store the session code
- 2020-03-31 20:30:45
- OfStack
Pitfall # 1: if the client machine's cookie is invalidated by a virus, then the session is effectively gone.
Session is stored in a temporary folder as a file by default in PHP, which is fine for a small system.
However, for a large and frequently accessed system, this is not a good idea. Let's say that this website gets 1,000 people a day. After a month, the session temporary folder will have 30,000 temporary files. Imagine how long it would take the computer to find a session_sid from 30,000!
So in order to improve efficiency.
Transactions use a database to save sessions. Specific methods are as follows:
1. Change the php.ini file.
Since the default way PHP saves sessions is files, we're going to change it. Find "session.save_handler = files" and change "files" to "User".
Change the session mode to user-defined.
2. Database establishment:
The CREATE TABLE ` db_session ` (
` sesskey ` char (32) NOT NULL,
'expiry' int(11) unsigned NOT NULL,
` value ` text NOT NULL,
PRIMARY KEY (` sesskey `)
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
[/ code]
The database says: db_session
Column name: sesskey, expiry, value
Inside Value is the Value in session.
3. Create a session_mysql.php file. This file is used to construct the method to save session. Modify the parameters can be used directly.
Session_mysql. PHP
The PHP code:
4. Create test files.
You must refer to the session_mysql.php file you just created before using it.
Session_test. PHP
The PHP code:
Get_session_test. PHP
Get_session_test2. PHP
Session is stored in a temporary folder as a file by default in PHP, which is fine for a small system.
However, for a large and frequently accessed system, this is not a good idea. Let's say that this website gets 1,000 people a day. After a month, the session temporary folder will have 30,000 temporary files. Imagine how long it would take the computer to find a session_sid from 30,000!
So in order to improve efficiency.
Transactions use a database to save sessions. Specific methods are as follows:
1. Change the php.ini file.
Since the default way PHP saves sessions is files, we're going to change it. Find "session.save_handler = files" and change "files" to "User".
Change the session mode to user-defined.
2. Database establishment:
The CREATE TABLE ` db_session ` (
` sesskey ` char (32) NOT NULL,
'expiry' int(11) unsigned NOT NULL,
` value ` text NOT NULL,
PRIMARY KEY (` sesskey `)
) ENGINE = InnoDB DEFAULT CHARSET = latin1;
[/ code]
The database says: db_session
Column name: sesskey, expiry, value
Inside Value is the Value in session.
3. Create a session_mysql.php file. This file is used to construct the method to save session. Modify the parameters can be used directly.
Session_mysql. PHP
The PHP code:
<?php
$gb_DBname="db_myBBS";//Database name
$gb_DBuser="root";//Database user name
$gb_DBpass="23928484";//Database password
$gb_DBHOSTname="localhost";//The name or IP address of the host
$SESS_DBH="";
$SESS_LIFE=get_cfg_var("session.gc_maxlifetime");//Get the maximum duration of the session.
function sess_open($save_path,$session_name){
global $gb_DBHOSTname,$gb_DBname,$gb_DBuser,$gb_DBpass,$SESS_DBH;
if(!$SESS_DBH=mysql_pconnect($gb_DBHOSTname,$gb_DBuser,$gb_DBpass)){
echo "<li>MySql Error:".mysql_error()."<li>";
die();
}
if(!mysql_select_db($gb_DBname,$SESS_DBH)){
echo "<li>MySql Error:".mysql_error()."<li>";
die();
}
return true;
}
function sess_close(){
return true;
}
function sess_read($key){
global $SESS_DBH,$SESS_LIFE;
$qry="select value from db_session where sesskey = '$key' and expiry > ".time();
$qid=mysql_query($qry,$SESS_DBH);
if(list($value)=mysql_fetch_row($qid)){
return $value;
}
return false;
}
function sess_write($key,$val){
global $SESS_DBH,$SESS_LIFE;
$expiry=time()+$SESS_LIFE;
$value=$val;
$qry="insert into db_session values('$key',$expiry,'$value')";
$qid=mysql_query($qry,$SESS_DBH);
if(!$qid){
$qry="update db_session set expiry=$expiry, value='$value' where sesskey='$key' and expiry >".time();
$qid=mysql_query($qry,$SESS_DBH);
}
return $qid;
}
function sess_destroy($key){
global $SESS_DBH;
$qry="delete from db_session where sesskey = '$key'";
$qid=mysql_query($qry,$SESS_DBH);
return $qid;
}
function sess_gc($maxlifetime){
global $SESS_DBH;
$qry="delete from db_session where expiry < ".time();
$qid=mysql_query($qry,$SESS_DBH);
return mysql_affected_rows($SESS_DBH);
}
session_module_name();
session_set_save_handler("sess_open","sess_close","sess_read","sess_write","sess_destroy","sess_gc");
?>
4. Create test files.
You must refer to the session_mysql.php file you just created before using it.
Session_test. PHP
The PHP code:
<?php
include ("session_mysql.php");
session_start();
$_SESSION['abc']= "A: I will be back!";
$_SESSION['meto']= "B: Me too ";
$_SESSION['name']= "louis ";
echo "<a href="get_session_test.php">click me</a>";
?>
Get_session_test. PHP
<?php
include ("session_mysql.php");
session_start();
echo $_SESSION['abc'];
echo "<br>";
echo $_SESSION['meto'];
echo "<br>";
echo $_SESSION['name'];
$_SESSION['wq']="12e";
echo "<br><a href="get_session_test2.php">click again</a>";
?>
Get_session_test2. PHP
<?php
include ("session_mysql.php");
session_start();
echo $_SESSION['abc'];
echo "<br>";
echo $_SESSION['meto'];
echo "<br>";
echo $_SESSION['name'];
echo "<br>";
echo $_SESSION['wq'];
//session_destroy();// To destroy all session The function.
?>