Home Database Mysql Tutorial session存储到mysql数据库_MySQL

session存储到mysql数据库_MySQL

May 31, 2016 am 08:46 AM

根据qq互联中的session类和php官方上的例子,整理

1

<?phpini_set (&#39;error_reporting&#39;,E_ALL);ini_set(&#39;display_errors&#39;,TRUE);class session{const db_host = &#39;127.0.0.1&#39;;const db_user = &#39;root&#39;;const db_password =&#39;&#39;;const db_name = &#39;test&#39;;const db_table = &#39;tbl_session&#39;;private $lifetime;private $dbHandle;function open($path,$id){  $con = @mysql_connect(self::db_host,self::db_user,self::db_password);  $sel = @mysql_select_db(self::db_name,$con);  $this->lifetime = get_cfg_var("session.gc_maxlifetime");  $this-&gt;dbHandle = $conif(!$con || !$sel)      return false;      return true; }function close(){    $this-&gt;gc($this-&gt;lifetime);  return @mysql_close($this-&gt;dbHandle);}function read($id){   $rs = @mysql_query("select session_data data from ".self::db_table." where session_expires dbHandle);    $row = @mysql_fetch_assoc($rs); return $row?$row['data']:'';}function write($id,$data){ $newEXP = time()+ ini_get("session.gc_maxlifetime");   $sel = mysql_query("select * from ".self::db_table." where session_id= '".$id."'",$this-&gt;dbHandle);   if(mysql_affected_rows($this-&gt;dbHandle)){     $rs = @mysql_query("update ".self::db_table." set session_data = '".$data."', session_expires = '".$newEXP."' WHERE session_id = '".$id."'",$this-&gt;dbHandle);     if(@mysql_affected_rows($this-&gt;dbHandle))        return true;   }else{     $rs = @mysql_query("insert into ".self::db_table." (session_id,session_data,session_expires)values('".$id."','".$data."','".$newEXP."')",$this-&gt;dbHandle);    if(@mysql_affected_rows($rs))       return true;   }   return false;}function destroy($id){    @mysql_query("delete from ".self::db_table." where session_id = '".$id."'",$this-&gt;dbHandle);}function gc($lifetime){    @mysql_query("delete from ".self::db_table." where sessoin_expires &gt; '".time()."'",$this-&gt;dbHandle);}}$session = new session();session_set_save_handler(array(&amp;$session,'open'),array(&amp;$session,'close'),array(&amp;$session,'read'),array(&amp;$session,'write'),array(&amp;$session,'destroy'),array(&amp;$session,'gc'));session_start();

Copy after login



Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

Reduce the use of MySQL memory in Docker

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

How do you alter a table in MySQL using the ALTER TABLE statement?

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

How to solve the problem of mysql cannot open shared library

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

What is SQLite? Comprehensive overview

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

Run MySQl in Linux (with/without podman container with phpmyadmin)

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

Running multiple MySQL versions on MacOS: A step-by-step guide

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)? Mar 18, 2025 pm 12:00 PM

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?

See all articles