doujieyu7062 2019-01-12 19:32 采纳率: 0%
浏览 190

docker-compose php laravel - session不会保持持久化状态

I've gotten a docker php laravel container working fine as per this: https://medium.com/@shakyShane/laravel-docker-part-1-setup-for-development-e3daaefaf3c

Now I am trying to set a session and it is not persisting.

I have a form post (LoginController.login) that creates a session and session_start() returns true and session_id() return an id as expected (like 74f0f68268bf48d50149ca344e2e4acc)

However when I then immediately go to /login, i.e. GET - LoginController.index, the session_id() is empty

Here are the relevant files/code/info

macOS mojave

docker Version 2.0.0.0-mac81 (29211)

container Laravel Framework version 5.3.31

container PHP 7.2.13

web.php

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

LoginController

/**GET returns login page if no session, home otherwise */

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');
    }
}

/**POST present login form if not authenticated, home otherwise*/

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

Added the following to index.php to see if it helps

ini_set('session.cookie_secure','Off');
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/tmp'));
  • 写回答

1条回答 默认 最新

  • dongnuoyi8833 2019-01-15 21:11
    关注

    session_start(); must be called every time to create/open the current session, I was wrongly thinking that session_start(); should be called once.

    评论

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备