【问题标题】:How do I create a custom session in Google AppEngine using Laravel 5.2?如何使用 Laravel 5.2 在 Google AppEngine 中创建自定义会话?
【发布时间】:2018-09-12 02:11:18
【问题描述】:

我在 AppEngine 中的会话遇到问题。出于某种原因,每次我登录到我的应用程序时,它每 30 到 60 分钟就会把我踢出去,即使我在 Laravel 5.2 的 session.php 中将生命周期设置为 720 分钟。我还创建了一个新的会话处理程序。以下是我的文件。

app\Extensions\AppEngineStorageSessionHandler.php

namespace App\Extensions;

class AppEngineStorageSessionHandler implements \SessionHandlerInterface{
private $savePath = '';

public function open($savePath,$sessionName){
    $this->savePath = 'http://example.com';
    if(!is_dir($this->savePath)){
        mkdir($this->savePath,0777);
    }
    return true;
}

public function close(){
    return true;
}

public function read($id){
    return (string)@file_get_contents($this->savePath.'/sess_'.$id);
}

public function write($id,$data){
    return (file_put_contents($this->savePath.'/sess_'.$id,$data) === false)?false:true;
}

public function destroy($id){
    $file = $this->savePath.'/sess_'.$id;
    if(file_exists($file)) {
        unlink($file);
    }
    return true;
}

public function gc($maxlifetime){
    foreach(glob($this->savePath.'/sess_*') as $file){
        if(filemtime($file) + $maxlifetime < time() && file_exists($file)){
            unlink($file);
        }
    }
    return true;
}

我为此处理程序创建了一个新的提供程序。

app\Providers\SessionServiceProvider.php

namespace App\Providers;

use App\Extensions\AppEngineStorageSessionHandler;
use Illuminate\Support\ServiceProvider;
use Session;

class SessionServiceProvider extends ServiceProvider{
/**
 * Bootstrap the application services.
 *
 * @return void
 */
public function boot()
{
    Session::extend('gae_session', function($app) {
        // Return implementation of SessionHandlerInterface...
        return new AppEngineStorageSessionHandler;
    });
}

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    //
}
}

最后,我将此提供程序添加到 config\app.php 下的 Providers 数组的最末尾。

'providers' => [
    ....
    App\Providers\SessionServiceProvider::class,
]

我错过了什么?我已经搜索了很多很多小时,但没有找到可行的解决方案。我还尝试在每一页上调用session_start();,但什么也没有。任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: php laravel google-app-engine session session-variables


    【解决方案1】:

    您将文件系统用于会话,但 App Engine 中的实例是短暂的。相反,您应该使用 cookie 会话或数据库会话。

    可以通过在app.yaml 中将SESSION_DRIVER 设置为cookie 来实现Cookie 会话:

    env_variables:
      SESSION_DRIVER: cookie
    

    Databsae 会话可以按照this tutorial的“设置数据库会话”一章来实现

    【讨论】:

      猜你喜欢
      • 2016-07-01
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 2016-11-03
      • 1970-01-01
      • 2017-07-27
      相关资源
      最近更新 更多