我已经按照以下说明正常工作了一个docker php laravel容器: https://medium.com/@shakyShane/laravel-docker-part-1-setup-for-development-e3daaefaf3c
现在,我正在尝试设置一个会话,并且该会话不持久。
我有一个表单会话(LoginController.login)创建一个会话,session_start()返回true,session_id()返回一个预期的ID(例如74f0f68268bf48d50149ca344e2e4acc)
但是,当我随后立即进入/ login,即GET-LoginController.index时,session_id()为空
以下是相关的文件/代码/信息
macOS mojave
docker版本2.0.0.0-mac81(29211)
容器Laravel Framework版本5.3.31
容器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如果没有会话,则返回登录页面,否则返回首页 * /
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'));
答案 0 :(得分:0)
session_start();必须在每次创建/打开当前会话时调用,我错误地认为session_start();应该被调用一次。