Php Slim SessionCookie中间件不工作
这是我的index.phpPhp Slim SessionCookie中间件不工作,php,session-cookies,middleware,slim,Php,Session Cookies,Middleware,Slim,这是我的index.php <?php $app = new \Slim\Slim( array( 'templates.path' => dirname(__FILE__).'/templates' ) ); // Add session cookie middle-ware. Shouldn't this create a cookie? $app->add(new \Slim\Middleware\SessionCookie()); /
<?php
$app = new \Slim\Slim(
array(
'templates.path' => dirname(__FILE__).'/templates'
)
);
// Add session cookie middle-ware. Shouldn't this create a cookie?
$app->add(new \Slim\Middleware\SessionCookie());
// Add a custom middle-ware
$app->add(new \CustomMiddleware());
$app->get(
'/',
function () use ($app) {
$app->render('Home.php');
}
);
$app->run();
?>
如果在Slim\Middleware\sessionokie
之前先添加CustomMiddleware
如何
大概是这样的:
require 'Slim/Slim.php';
Slim\Slim::registerAutoloader();
class CustomMiddleware extends Slim\Middleware
{
public function call() {
// This session variable should be saved
$_SESSION['test'] = 'Hello!';
$this->next->call();
}
}
$app = new Slim\Slim();
$app->add(new CustomMiddleware());
$app->add(new Slim\Middleware\SessionCookie());
// GET route
$app->get('/', function () use ($app)
{
$app->render('home.php');
});
$app->run();
以及您的home.php
模板文件:
<?php echo($_SESSION['test']); ?>
对我来说,它完美无瑕。但是如果我在我的CustomMiddleware
之前添加Slim\Middleware\SessionOkie
,那么$\u会话['test']
的输出将保持NULL
中间件就是这样工作的:
因此,您的响应将永远不会得到任何$\u SESSION
值,因为您在调用sessionokie
之前设置了$\u SESSION
。我以前没有使用过slim,但这可能是声明SESSION\u start()的简单情况在设置任何变量之前,您可以确定调用方法调用SessionCookie与CustomMiddleware的顺序吗?查看源代码,如果cookie未设置,会话将重置。我发现这一行:ini\u set('SESSION.use\u cookies',0)代码>。看起来Cookie的使用被禁用了。CustomMiddleware在SessionOkie之前调用,但即使我在SessionOkie之后调用它,会话在我刷新后也不会持续。您解决了吗?我也有同样的问题。
require 'Slim/Slim.php';
Slim\Slim::registerAutoloader();
class CustomMiddleware extends Slim\Middleware
{
public function call() {
// This session variable should be saved
$_SESSION['test'] = 'Hello!';
$this->next->call();
}
}
$app = new Slim\Slim();
$app->add(new CustomMiddleware());
$app->add(new Slim\Middleware\SessionCookie());
// GET route
$app->get('/', function () use ($app)
{
$app->render('home.php');
});
$app->run();
<?php echo($_SESSION['test']); ?>