Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the configuration methods of using memcache to store session in PHP

Shulou Source: shulou.com Published: 2022-06-03 07:51:05 11月15日 Update

This article mainly explains "what are the configuration methods of using memcache to store session in PHP", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what are the configuration methods of using memcache to store session in PHP?"

1. Modify the php.ini configuration file directly

The copy code is as follows:

Session.save_handler = memcache / / set the storage mode of session to memcache

Memcache.hash_strategy = "consistent" / / sets the hash algorithm for memcache

Session.save_path = "tcp://127.0.0.100:11211" / / sets the location where session is stored, and multiple memcache are separated by commas, for example: tcp://127.0.0.1:11211,tcp://127.0.0.1:12000

2. Configure using the .htaccess file in the directory

The copy code is as follows:

Php_value session.save_handler "memcache"

Php_value session.save_path "tcp://127.0.0.1:11211"

Note: this is only for Apache. At present, many people use Nginx, and this method is not recommended.

3. Modify the configuration in the PHP file of the project

The copy code is as follows:

Ini_set ("session.save_handler", "memcache")

Ini_set ("session.save_path", "tcp://127.0.0.100:11211")

4. Test examples

The copy code is as follows:

/ / Test whether the session read is normal

Session_start ()

$_ SESSION ['username'] = "jb51.net"

Echo session_id ()

/ / read session from Memcache

M = new Memcache ()

M-> connect ('localhost', 11211)

/ / or this

/ / $mem- > addServer ("127.0.0.1", 11211) or die ("Can't add Memcache server 127.0.0.1 or die")

/ / obtain data according to session_id

/ / this machine

/ / $session = $m-> get (session_id ()); / / session_id:d527b6f983bd5e941f9fff318a31206b

/ / another server, known session id

$session = $m-> get ("d527b6f983bd5e941f9fff318a31206b")

Echo $session. ""; / / you will get the following data: username | Svv 16: "pandao";, you can get the corresponding value by parsing it.

Echo session_id (). "

Exit

At this point, I believe you have a deeper understanding of "what are the configuration methods of using memcache to store session in PHP?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Code configuration method storage file content data way learning testing practical deeper location example interest just multiple practical practical easy to operate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Xiaomi Redmi NVidia Huawei