我决定迁移我的系统并进行一些急需的更新。我使用 Codeigniter 3 和 php 7.2 遇到了这个问题。在我发现这个问题后,我意识到它是多么可笑,并想知道我怎么没有早点弄清楚。
无论如何,至少对我来说这是解决方案。
显然确保已安装 memcacheD:
sudo apt-get update
sudo apt-get install php7.2-memcached
如果这很好,那么我们可以继续检查其他所有内容。
在 Codeigniter 的配置文件“/application/config/config.php”中有一个部分可以指定会话选项:
$config['sess_driver'] = 'memcached';
$config['sess_cookie_name'] = 'some_session_name';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
需要为我更改的行是这一行:
$config['sess_save_path'] = NULL;
确保将其设置为 CI3 手册中记录的有效路径。正确的设置如下所示:
$config['sess_driver'] = 'memcached';
$config['sess_save_path'] = 'localhost:11211';
确保更改“localhost”以反映您的 memcached 服务器位置。
有关更多信息,请参阅 - Codeignier 会话手册
也看看这里:php memcacheD Sessions support
如果页面上的评论被删除,我会在这里发布:
如果你想使用'memcacheD'扩展而不是'memcache'(有两个不同的扩展)进行会话控制,你应该注意修改php.ini
谷歌的大多数网络资源都基于 memcache,因为它的版本比 memcacheD 更早。他们会说如下
session.save_handler = memcache session.save_path = "tcp://localhost:11211"
但是当涉及到 memcacheD 时它是无效的
你应该像这样修改 php.ini
session.save_handler = memcached session.save_path = "localhost:11211"
看,没有协议标识符
为了进行测试,我这样做是为了让用户认为 php 它本身在访问 memcacheD 时没有问题:
session_start();
header('Content-Type: text/plain');
session_start();
if(!isset($_SESSION['visit']))
{
echo "This is the first time you're visiting this server\n";
$_SESSION['visit'] = 0;
}
else
echo "Your number of visits: ".$_SESSION['visit'] . "\n";
$_SESSION['visit']++;
echo "Server IP: ".$_SERVER['SERVER_ADDR'] . "\n";
echo "Client IP: ".$_SERVER['REMOTE_ADDR'] . "\n";
print_r($_COOKIE);
$servers = explode(",", ini_get("session.save_path"));
$c = count($servers);
for ($i = 0; $i < $c; ++$i) {
$servers[$i] = explode(":", $servers[$i]);
}
$mem = new memcached();
$mem->addServer('127.0.0.1', '11211', '1');
$mem->set('011', 'Hello There');
print_r($mem->get('011'));
print_r($mem->getAllKeys());
这让我看到 memcacheD 工作正常。
在您的 php.ini 中也有一些选项需要注意。只需搜索 [session] 或 session.save_path。CI3 声明它不使用 php.ini 文件中的此选项,但如果您计划在框架之外使用 memcacheD 并保持一致性,则值得设置。
这从 php.ini 文件中的 ~1327 行开始:
[Session]
; Handler used to store/retrieve data.
; http://php.net/session.save-handler
session.save_handler = memcached
; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
;
; The path can be defined as:
;
; session.save_path = "N;/path"
;
; where N is an integer. Instead of storing all the session files in
; /path, what this will do is use subdirectories N-levels deep, and
; store the session data in those directories. This is useful if
; your OS has problems with many files in one directory and is
; a more efficient layout for servers that handle many sessions.
;
; NOTE 1: PHP will not create this directory structure automatically.
; You can use the script in the ext/session dir for that purpose.
; NOTE 2: See the section on garbage collection below if you choose to
; use subdirectories for session storage
;
; The file storage module creates files using mode 600 by default.
; You can change that by using
;
; session.save_path = "N;MODE;/path"
;
; where MODE is the octal representation of the mode. Note that this
; does not overwrite the process's umask.
; http://php.net/session.save-path
;session.save_path = "/var/lib/php/sessions"
session.save_path = "locahost:11211"
检查设置的另一个地方是 mecacheD 服务器配置文件。根据系统,它的位置可能会有所不同。对我来说,它位于/etc/目录下。
/etc/memcached.conf
图片供参考我得到的错误:
编辑
如果您使用套接字即 unix:///some/place/memcached.sock 连接到 Memcached 服务器,则没有关于如何使其工作的确切文档。您只需在配置中执行以下操作:
$config['sess_save_path'] = "/some/place/memcached.sock:11211";