How to operate session and database in php

墨辰丷
Release: 2023-03-31 08:40:02
Original
1826 people have browsed it

This article mainly introduces the method of operating session and database in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The example in this article describes how PHP uses MySQL to save sessions. The specific analysis is as follows:

This function is generally available in many large systems, but it must be separated and analyzed, and the information on the Internet There are not too many. Here I compiled an article to share with you

Using MySQL to save sessions has many advantages over files:

1) It is beneficial to distributed systems, files can only be saved On one machine
2) It is beneficial to systems with large traffic. When using files, each session is saved in a file. The directory will be very large and it will be difficult to find the session file.

To use MySQL to save a session, you must first create the session table:

Copy after login

MysqlSession class is as follows:

DB_NAME = &$sessionDB; $this->SESS_LIFE = get_cfg_var("session.gc_maxlifetime"); session_module_name('user'); session_set_save_handler( array(&$this, 'sess_open'), array(&$this, 'sess_close'), array(&$this, 'sess_read'), array(&$this, 'sess_write'), array(&$this, 'sess_destroy'), array(&$this, 'sess_gc') ); session_start(); } function sess_open($save_path, $session_name){ // 打开数据库连接 if (! $this->DB_SELECT_DB = mysql_pconnect($this->DB_SERVER, $this->DB_USER, $this->DB_PASS)) { echo "SORRY! MYSQL ERROR : Can't connect to $this->DB_SERVER as $DB_USER"; echo "MySQL Error: ", mysql_error(); die; } if (! mysql_select_db($this->DB_NAME, $this->DB_SELECT_DB)) { echo "SORRY! MYSQL ERROR : Unable to select database $this->DB_NAME"; die; } return true; } function sess_close() { return true; } function sess_read($SessionKey){ $Query = "SELECT SessionArray FROM sessions WHERE SessionKey = '".$SessionKey."' AND SessionExpTime > " . time(); // 过期不读取。 $Result = mysql_query($Query, $this->DB_SELECT_DB); if (list($SessionArray) = mysql_fetch_row($Result)) { return $SessionArray; } return false; } function sess_write($SessionKey, $VArray) { $SessionExpTime = time() + $this->SESS_LIFE; // 更新Session过期时间,Session过期时间 = 最后一次更新时间 + Session的最大使用时长 $SessionArray = addslashes($VArray); $Query = "INSERT INTO sessions (SessionKey,SessionExpTime,SessionArray) VALUES ('".$SessionKey."','".$SessionExpTime."','".$SessionArray."')"; $Result = mysql_query($Query, $this->DB_SELECT_DB); if (!$Result){ $Query = "UPDATE sessions SET SessionExpTime = '".$SessionExpTime."', SessionArray = '".$SessionArray."' WHERE SessionKey = '".$SessionKey."' AND SessionExpTime > " . time(); $Result = mysql_query($Query, $this->DB_SELECT_DB); } return $Result; } function sess_destroy($SessionKey) { $Query = "DELETE FROM sessions WHERE SessionKey = '".$SessionKey."'"; $Result = mysql_query($Query, $this->DB_SELECT_DB); return $Result; } function sess_gc($maxlifetime) { // 这个垃圾清除器系统调用。默认是1440秒清除一次。 //参数可以在PHP.ini里面设置。 $Query = "DELETE FROM sessions WHERE SessionExpTime < " . time(); $Result = mysql_query($Query, $this->DB_SELECT_DB); return mysql_affected_rows($this->DB_SELECT_DB); } } ?>
Copy after login

Usage: Where session_start was originally used, replace it with $session = new MysqlSession ()

Note: The database must be opened before including this program. The database cannot be closed before the main program exits, otherwise an error will occur.

Summary:The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP Mysql jQuery implements password retrieval function

php implements loading fonts and Save

PHP implements Chinese character verification code

The above is the detailed content of How to operate session and database in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!