Home >Backend Development >PHP Tutorial >PHP method to save session to memcache server_php tips

PHP method to save session to memcache server_php tips

WBOY
WBOYOriginal
2016-05-16 20:00:031090browse

The example in this article describes the method of saving session to memcache server in PHP. Share it with everyone for your reference, the details are as follows:

The traditional session is written to the server file, which can be seen in php.ini. The list is as follows

session.save_handler = files
session.save_path = "sess保存路径"

However, if the website has many users, session access will inevitably affect the speed of the website. Because the file reading speed is very low.

As we all know, memcache, as a memory cache server, reads data in the form of key->value through a hash algorithm, and its speed is much higher than reading files.

The configuration to save the session to the memcache server is as follows:

Method 1:

Open the php.ini file and modify the following two parameters:

session.save_handler = memcache
session.save_path = "tcp://Mem服务器1:端口号,tcp://Mem服务器2:端口号..."

Method 2:

Use the ini_set function in the php file to configure. This method will solve the php configuration problem of the shared server

<&#63;php
....
ini_set("session.save_handler", "memcache");
ini_set("session.save_path", "tcp://Mem服务器1:端口号,tcp://Mem服务器2:端口号...");
....
&#63;>

Restart the web server!

At this time, the session will not be saved in the form of a file, but will be saved to the Memcache server. The saved key is session_id

Telnet to the memcache server and get the view

telnet memcache server port number

Readers who are interested in more content related to PHP caching can check out the special topic of this site: "Summary of PHP caching technology"

I hope this article will be helpful to everyone in PHP programming.

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