GAE gcloud dev_appserver.py PHP:无法读取会话数据:用户(路径:Memcache)
GAE gcloud dev_appserver.py PHP: Failed to read session data: user (path: Memcache)
我 运行 我的 PHP (runtime: php55)
应用程序的本地 Google Cloud App Engine 模拟器。它有效,但 PHP 会话除外。我收到以下消息:
Warning: session_start(): Failed to read session data: user (path: Memcache)
我使用以下命令启动应用程序
dev_appserver.py --php_executable_path=/usr/bin/php-cgi ./default
所以我 运行 使用 php-cgi。在此之前,我尝试使用常规 php 运行,但后来我得到了 WSOD。在 Google 小组中,有人建议使用 php-cgi,它为我解决了这个问题。但是现在还是有这个问题,好像跟Memcache有关。
这是在 Linux Mint (Ubuntu) 上,这个问题没有发生在 Windows 机器上,我有相同的应用程序 运行ning模拟器。
当我安装 php-memcache 时,我无法再启动该应用程序。 运行在安装了 php-memcache 的情况下执行上述命令时,出现此错误:
PHPEnvironmentError: The PHP runtime cannot be run with the
"Memcache" PECL extension installed
我该如何解决?
我没有用 PHP cgi 解决问题,但我通过编写自己的会话处理程序解决了这个问题。默认情况下,GAE 使用 'user' 会话处理程序将会话存储在 Memcache 中。如果由于某种原因这不起作用,您可以使用我的以下代码将本地 GAE 切换到 'files' 会话处理程序并将会话存储在文件夹中:
<?php
if ($_SERVER['SERVER_NAME'] == 'localhost') {
class FileSessionHandler {
private $savePath;
function open($savePath, $sessionName) {
$this->savePath = $savePath;
if (!is_dir($this->savePath)) {
mkdir($this->savePath, 0777);
}
return true;
}
function close() {
return true;
}
function read($id) {
return (string) @file_get_contents("$this->savePath/sess_$id");
}
function write($id, $data) {
return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true;
}
function destroy($id) {
$file = "$this->savePath/sess_$id";
if (file_exists($file)) {
unlink($file);
}
return true;
}
function gc($maxlifetime) {
foreach (glob("$this->savePath/sess_*") as $file) {
if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
unlink($file);
}
}
return true;
}
}
$handler = new FileSessionHandler();
session_set_save_handler(
array($handler, 'open'), array($handler, 'close'), array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc')
);
session_save_path('[PATH_TO_WRITABLE_DIRECTORY]');
register_shutdown_function('session_write_close');
}
session_start();
首先,在努力解决与您相同的问题时,我发现 here:
dev_appserver.py is not supported with the PHP 7.2 runtime. To test your application and run it locally, you must download and install PHP 7.2 and set up a web server.
话虽如此,php-cgi & java 似乎是 运行,但有一些差异,内存缓存扩展确实必须在 php.ini
中禁用,但是运行时注册了 Memcached
class 所以这应该在开发和 App Engine 环境中工作:
extension_loaded('memcached') || class_exists('Memcached')
回到你的问题,我在开发模式下解决了会话错误:
ini_set('session.save_handler', 'files');
ini_set('session.save_path', null);
我 运行 我的 PHP (runtime: php55)
应用程序的本地 Google Cloud App Engine 模拟器。它有效,但 PHP 会话除外。我收到以下消息:
Warning: session_start(): Failed to read session data: user (path: Memcache)
我使用以下命令启动应用程序
dev_appserver.py --php_executable_path=/usr/bin/php-cgi ./default
所以我 运行 使用 php-cgi。在此之前,我尝试使用常规 php 运行,但后来我得到了 WSOD。在 Google 小组中,有人建议使用 php-cgi,它为我解决了这个问题。但是现在还是有这个问题,好像跟Memcache有关。
这是在 Linux Mint (Ubuntu) 上,这个问题没有发生在 Windows 机器上,我有相同的应用程序 运行ning模拟器。
当我安装 php-memcache 时,我无法再启动该应用程序。 运行在安装了 php-memcache 的情况下执行上述命令时,出现此错误:
PHPEnvironmentError: The PHP runtime cannot be run with the
"Memcache" PECL extension installed
我该如何解决?
我没有用 PHP cgi 解决问题,但我通过编写自己的会话处理程序解决了这个问题。默认情况下,GAE 使用 'user' 会话处理程序将会话存储在 Memcache 中。如果由于某种原因这不起作用,您可以使用我的以下代码将本地 GAE 切换到 'files' 会话处理程序并将会话存储在文件夹中:
<?php
if ($_SERVER['SERVER_NAME'] == 'localhost') {
class FileSessionHandler {
private $savePath;
function open($savePath, $sessionName) {
$this->savePath = $savePath;
if (!is_dir($this->savePath)) {
mkdir($this->savePath, 0777);
}
return true;
}
function close() {
return true;
}
function read($id) {
return (string) @file_get_contents("$this->savePath/sess_$id");
}
function write($id, $data) {
return file_put_contents("$this->savePath/sess_$id", $data) === false ? false : true;
}
function destroy($id) {
$file = "$this->savePath/sess_$id";
if (file_exists($file)) {
unlink($file);
}
return true;
}
function gc($maxlifetime) {
foreach (glob("$this->savePath/sess_*") as $file) {
if (filemtime($file) + $maxlifetime < time() && file_exists($file)) {
unlink($file);
}
}
return true;
}
}
$handler = new FileSessionHandler();
session_set_save_handler(
array($handler, 'open'), array($handler, 'close'), array($handler, 'read'), array($handler, 'write'), array($handler, 'destroy'), array($handler, 'gc')
);
session_save_path('[PATH_TO_WRITABLE_DIRECTORY]');
register_shutdown_function('session_write_close');
}
session_start();
首先,在努力解决与您相同的问题时,我发现 here:
dev_appserver.py is not supported with the PHP 7.2 runtime. To test your application and run it locally, you must download and install PHP 7.2 and set up a web server.
话虽如此,php-cgi & java 似乎是 运行,但有一些差异,内存缓存扩展确实必须在 php.ini
中禁用,但是运行时注册了 Memcached
class 所以这应该在开发和 App Engine 环境中工作:
extension_loaded('memcached') || class_exists('Memcached')
回到你的问题,我在开发模式下解决了会话错误:
ini_set('session.save_handler', 'files');
ini_set('session.save_path', null);