Home  >  Article  >  Backend Development  >  PHP uses mysql database to store session_PHP tutorial

PHP uses mysql database to store session_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:34:53930browse

Most people who use PHP will use cookies once they are applied to sessions.
Although cookies are good, they can also bring us some hidden dangers.
Hidden danger 1: If the cookie of the client machine becomes invalid due to a virus, then the session is equivalent to being gone.
Hidden danger 2: The session is saved in a temporary folder in the form of a file by default in PHP. For a small system, this is perfectly fine,
But for a large and frequently accessed system, this is not a good solution. Suppose this website is visited by 1,000 people a day. After one month, the temporary folder of the session will have 30,000 temporary files. Imagine how long it takes for the computer to find a session_sid from 30000!
So in order to improve efficiency.
The transaction uses a database to save the session. The specific method is as follows:
1. Change the php.ini file.
Since the default way of saving sessions in PHP is files, we need to change it. That is: find "session.save_handler = files" and change "files" to "User".
Change the session mode to user-defined.
2. Create a database:
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;
Database shows: db_session
Column name: sesskey, expiry, value Where: sesskey is the primary key.
Value stores the value in the session.
3. Create the session_mysql.php file. This file is used to construct a method to save the session. Just modify the parameters and use it directly.
session_mysql.php

Copy PHP content to clipboard
PHP code:

​​​​ $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 validity period 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 "

  • MySql Error:".mysql_error()."
  • ";
                          die();
                    }
    If(!mysql_select_db($gb_DBname,$SESS_DBH)){
    echo "
  • MySql Error:".mysql_error()."
  • ";
                          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();
        &n

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/508429.htmlTechArticle大部分使用php的人一旦应用到session都会使用cookie。 cookie虽好可是它也会给我们带来一些隐患的。 隐患一:如果客户端机器的cookie一旦因病...
  • 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