2017-01-10 41 views
0

我为我的PHP (runtime: php55)应用程序运行本地Google云应用程序引擎模拟器。它工作,除了PHP会话。我得到以下信息:GAE gcloud dev_appserver.py PHP:无法读取会话数据:用户(路径:Memcache)

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 Group中,建议使用php-cgi来解决这个问题。但是现在我仍然有这个问题,这似乎与Memcache有关。

这是在Linux Mint(Ubuntu)上的问题,并且在模拟器中运行相同应用程序的Windows机器上未出现此问题。

当我安装php-memcache时,我无法再启动应用程序。当安装php-memcache运行上述命令时,出现此错误:

PHPEnvironmentError: The PHP runtime cannot be run with the 
"Memcache" PECL extension installed 

如何解决此问题?

回答

0

我没有解决PHP CGI的问题,但我通过编写自己的会话处理程序来解决它。 GAE默认使用'用户'会话处理程序将会话存储在Memcache中。如果由于某种原因不起作用,您可以使用我的以下代码将本地GAE切换到“文件”会话处理程序,并将会话存储在一个文件夹中:

<?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();