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