Py学习  »  docker

docker compose php laravel-会话不保持持久性

Jeff I • 4 年前 • 522 次点击  

我已经让一个docker php laravel容器运行良好,如下所示: https://medium.com/@shakyShane/laravel-docker-part-1-setup-for-development-e3daaefaf3c

现在我试图设置一个会话,但它没有持续。

我有一个表单post(logincontroller.login)用于创建会话,session_start()返回true,session_id()按预期返回id(如74f0f68268bf48d50149ca344e2e4acc)

但是,当我立即转到/login,即get-logincontroller.index时,会话id()为空

以下是相关文件/代码/信息

马科斯莫哈韦

docker版本2.0.0.0-mac81(29211)

容器Laravel框架版本5.3.31

容器php 7.2.13

PHP

Route::get('/', function () {
    return view('welcome');
});
Route::get('home', 'HomeController@index');
Route::post('login', 'Auth\LoginController@login');
Route::get('login', 'Auth\LoginController@index');

登录控制器

/**如果没有会话,get返回登录页,否则返回主页 */

public function index()
{
    $sessionId = session_id();

    if (isset($sessionId) && !empty($sessionId)) {
        Utils::debug_to_console("cool, session id =   " .$sessionId);
        return view('home');
    }else{
        Utils::debug_to_console("doh! session id is fake news");
        return view('auth/login');
    }
}

/**如果未经认证,请张贴当前登录表单,否则请返回主页*/

public function login()
{
    $realId = session_id();

    if (isset($realId) && !empty($realId)) {
        return view('home');
    }
    try {
        $email = request('email');
        $password = request('password');
        if( isset($email) && isset($password) ){
            if( $email == 'foo@bar.com' && $password == 'foo' ){
                $start = session_start();
                $sessionId = session_id();
                Utils::debug_to_console("session_start " . $start);
                Utils::debug_to_console("sessionId  " .$sessionId);
                return view('home');
            }
        }
    } catch (Exception $e) {
        Utils::debug_to_console("ugh...  " .$e->getMessage());

    }
    $data['err'] = 'Invalid email/password';
    return view('auth/loginerror',$data);
}

docker-compose.yml文件

version: '2'
services:

  # The Application
  app:
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ./:/var/www
    environment:
      - "DB_PORT=3306"
      - "DB_HOST=database"

  # The Web Server
  web:
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes_from:
      - app
    ports:
      - 8080:80


volumes:
  .:

app.dockerfile文件

FROM php:7.2-fpm

RUN apt-get update && apt-get install -y libmcrypt-dev

 # Fix sh
RUN rm /bin/sh && ln -s /bin/bash /bin/sh

 #Install Xdebug
RUN pecl install xdebug
RUN docker-php-ext-enable xdebug



RUN echo "xdebug.remote_enable = 1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
  && echo "xdebug.remote_autostart = 1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
  && echo "xdebug.remote_host = docker.for.mac.localhost" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
  && echo "xdebug.remote_port = 9001" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
  && echo "xdebug.remote_log = /tmp/xdebug.log" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
  && echo "xdebug.idekey = VSCODE" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN usermod -u 1000 www-data

在index.php中添加了以下内容以查看它是否有帮助

ini_set('session.cookie_secure','Off');
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/tmp'));
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40722
 
522 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Jeff I
Reply   •   1 楼
Jeff I    5 年前

每次创建/打开当前会话时都必须调用session_start();我错误地认为应该调用session_start();一次。