Home  >  Article  >  Backend Development  >  PHP database save session session

PHP database save session session

不言
不言Original
2018-04-03 11:10:342660browse

This article introduces about saving sessions in PHP database. Now I share it with everyone. It can also be a reference for friends in need. Let’s take a look together


Foreword:


By default, PHP will save all session data in text files on the server. These files are usually saved on the server. Inside the temporary directory on.

Then why do we need to save the session in the database?

  1. The main reason: to improve the security of the system. On a shared server, without special settings, all websites will use the same temporary directory, which means that dozens of programs are reading and writing files in the same location. Not only was the speed slowed down, but it was also possible for someone else to steal my site's user data.

  2. Saving session data to the database can also make it easier to search for more information about web site sessions. We can query the number of active sessions (the number of users online at the same time), and we can also query Session data is backed up.

  3. If my site runs on multiple servers at the same time, then a user may send multiple requests to different servers during a session, but if the session data is saved in On a certain server, other servers cannot use these session data. If one of my servers only plays the role of a database, wouldn't it be very convenient for you to save all session data in the database?

For more understanding of PHP session, please refer to this blog. Thoroughly understand PHP’s SESSION mechanism

1. Create a session table

Since the session data is stored on the server, and an index (sessionID) is stored on the client, this index corresponds to a certain piece of session data on the server. Therefore, the two fields that the table must contain are id and data, and the session will have expiration time, so there is another field here which is last_accessed. Here I build the table under the test database:

CREATE TABLE sessions(
    id CHAR(32) NOT NULL,
    data TEXT,
    last_accessed TIMESTAMP NOT NULL,    PRIMARY KEY(id)
);

PHP database save session session

PS: If the program needs to save a large amount of data in the session, the data field may need to be defined as MEDIUMTEXT or LONGTEXT type.

2、定义会话函数:

这里我们主要有两个步骤:

  1. 定义与数据库交互的函数

  2. 使PHP能使用这些自定义函数

在第二步中,是通过调用函数 session_set_save_handler()来完成的,调用它需要6个参数,分别是 open(启动会话)、close(关闭会话)、read(读取会话)、write(写入会话)、destroy(销毁会话)、clean(垃圾回收)。

我们新建php文件 sessions.inc.php ,代码如下:


PS:

  1. 处理“读取”函数外,其他函数必须返回一个布尔值,“读取”函数必须返回一个字符串。

  2. .每次会话启动时,“打开”和“读取”函数将会立即被调用。当“读取”函数被调用的时候,可能会发生垃圾回收过程。

  3. 当脚本结束时,“写入”函数就会被调用,然后就是“关闭”函数,除非会话被销毁了,而这种情况下,“写入”函数不会被调用。但是,在“关闭”函数之后,“销毁”函数将会被调用。

  4. .session_set_save_handler()函数参数顺序不能更改,因为它们一一对应 open 、close、read、、、、

  5. 会话数据最后将会以数据序列化的方式保存在数据库中。

3、使用新会话处理程序

使用新会话处理程序只是调用session_set_save_handler()函数,使我们的自定义函数能够被自动调用而已。其他关于会话的操作都没有发生变化(以前怎么用现在怎么用,我们的函数会在后台自动被调用),包括在会话中存储数据,访问保存的会话数据以及销毁数据。

在这里,我们新建 sessions.php 文件,该脚本将在没有会话信息时创建一些会话数据,并显示所有的会话数据,在用户点击 ‘log out’(注销)时销毁会话数据。

代码:


    
    DB session test
Session data stored

"; }else{ echo "

Session data exists:

".print_r($_SESSION,1)."
"; }if(isset($_GET['logout'])){    //销毁会话数据     session_destroy();    echo "

session destroyed

"; }else{    echo "log out"; }echo "

session data :

".print_r($_SESSION,1)."
";echo '

The above is the detailed content of PHP database save session session. For more information, please follow other related articles on the PHP Chinese website!

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