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
<?php .... ini_set("session.save_handler", "memcache"); ini_set("session.save_path", "tcp://Mem服务器1:端口号,tcp://Mem服务器2:端口号..."); .... ?>
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.