2017-01-10 40 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();