PHP 7.4.0RC2 Released!


Memcached は独自のセッションハンドラを用意しており、 これを使用するとユーザーセッションを memcache に格納することができます。 内部的に使用するのは完全に分離された memcached インスタンスなので、 必要に応じて別のサーバープールを使用することができます。 セッションのキーは、プレフィックス memc.sess.key. をつけて格納されます。 同じサーバープールでセッションと一般のキャッシュを両方使用する場合は注意しましょう。

session.save_handler string

memcached を設定すると、セッションのサポートが有効となります。

session.save_path string

ホスト名:ポート 形式のエントリをカンマ区切りで指定します。 これをセッションサーバープールとして使用します。たとえば "sess1:11211, sess2:11211" のようになります。

add a note add a note

User Contributed Notes 10 notes

nfoo at naver dot com
9 years ago
If you want to use 'memcacheD' extention not 'memcache' (there are two diffrent extentions) for session control,  you should pay attention to modify php.ini

Most web resource from google is based on memcache because It's earlier version than memcacheD. They will say as following

session.save_handler = memcache
session.save_path = "tcp://localhost:11211"

But it's not valid when it comes to memcacheD

you should modify php.ini like that

session.save_handler = memcached
session.save_path = "localhost:11211"

Look, there is no protocol indentifier
Ian Maddox
5 years ago
If you are setting data to the session and it immediately disappears and you aren't getting any warnings in your PHP error log, it's probably because your sessions expired sometime in the 1970s.

Somewhere between memcached 1.0.2 and 2.1.0, the memcached session handler became sensitive to the 30-day TTL gotcha (aka "transparent failover").  If your session.gc_maxlifetime is greater than 2592000 (30 days), the value is treated as a unix timestamp instead of a relative seconds count.

This issue is likely to impact anyone with long-running sessions who is upgrading from Ubuntu 12.04 to 14.04.
taubers at gmail dot com
6 years ago
If you are using the memcache class for session handling your key is the PHP session ID.  This is different than when using the  memcached class.

Example with memcache:
GET nphu2u8eo5niltfgdbc33ajb62

Example with memcached:
GET memc.sess.key.nphu2u8eo5niltfgdbc33ajb62

For memcached, the prefix is set in the config:
memcached.sess_prefix = "memc.sess.key."
richard at fussenegger dot info
6 years ago
The documentation is not complete, you can also pass the weight of each server and you can use sockets if you want. In your PHP ini:


// Sockets with weight in the format socket_path:port:weight
session.save_path = "/path/to/socket:0:42"

// Or more than one so that weight makes sense?
session.save_path = "/path/to/socket_x:0:42,/path/to/socket_y:0:666"


And if you should ever want to access these servers in PHP:


= explode(",", ini_get("session.save_path"));
$c = count($servers);
for (
$i = 0; $i < $c; ++$i) {
$servers[$i] = explode(":", $servers[$i]);
$memcached = new \Memcached();
call_user_func_array([ $memcached, "addServers" ], $servers);

Andrei Darashenka
10 years ago
This extension supports Session-locking!

by default
MEMC_SESS_LOCK_WAIT       100000
benoit dot delmotte at gmail dot com
2 years ago
in case of multiples memcached servers,
the separator is a semicolon ( ; ) not a comma as written

session.save_path = "sess1:11211; sess2:11211"
velazcomtz dot miguel at gmail dot com
10 months ago
Is important to address that memcached is not concurrent just as regular PHP sessions.

If you have two tabs and one of them takes too long to respond and try to log out on the second, the memcached server won't respond.
madalin at mgiworx dot co dot uk
4 years ago
short mention: Memcached has authentication support.
sstratton at php dot net
8 years ago
While the previous poster has a point that Memcached can and will cleanup to make room for it's keys, the likelihood of active sessions (due to the likelihood that they will be written to again within 30 seconds) is fairly low provided you have your memory allocation properly alloted.
me at nileshgr dot com
5 years ago
It seems a few people think saving sessions in memcache is more secure compared to saving in file-system (/tmp) especially when the discussion is about shared hosting.

This is not true. memcache has NO authentication; everybody & anybody can read session.save_path (which will contain the memcached daemon address).

FastCGI (PHP-FPM) is much much better when you're considering security and shared hosting.

If you want to share sessions across multiple servers transparently without having headaches of adding custom handlers, you can use NFS or something similar.
To Top