Cookie不会在Slim Framework中持久存在

时间:2014-06-30 10:52:39

标签: php cookies

我是Slim框架的新手。我正在尝试使用中间件功能进行一些身份验证。但是,虽然我可以在一条路线上设置cookie,但它会在页面刷新时消失。

       $app->get('/demo', function () use ($app) {
      try {
      $app->setCookie('uid', 'blah', '2 Days');
print_r($app);
      } catch (Exception $e) {
      $app->response()->status(400);
      $app->response()->header('X-Status-Reason', $e->getMessage());
      }
      });
      $app->run();

执行print_r表示已设置cookie

[cookies] => Slim\Http\Cookies Object ( [defaults:protected] => Array ( [value] => [domain] => [path] => [expires] => [secure] => [httponly] => ) [data:protected] => Array ( [uid] => Array ( [value] => blah [domain] => [path] => / [expires] => 2 Days [secure] => [httponly] => ) [key] => Array ( [value] => blah [domain] => [path] => / [expires] => 2 Days [secure] => [httponly] => ) ) ) [body:protected] => [length:protected] => 0 ) )

或者至少setCookie正在提升价值。 但是,如果我然后去我的其他路线

     $app->get('/attractions', function () use ($app) {
          try {
print_r($app);
          $data = R::find('attractions');
          $app->render('attractions.php', array(
          'page_title' => "Attractions",
          'data' => $data
          )
          );
          } catch (Exception $e) {
          $app->response()->status(400);
          $app->response()->header('X-Status-Reason', $e->getMessage());
          }
          });

然后饼干不在那里。同样在我的firebug控制台的日志中,没有显示我设置的cookie。

知道问题可能是什么?我在网上发现了几个帖子,如http://help.slimframework.com/discussions/problems/652-cant-seem-to-get-php-cookies-working

但到目前为止我无法弄清楚这个问题。我正在运行MAMP,我已经为该域设置了一个虚拟主机。

启用了Mcrypt php扩展程序。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我在使用带有Cookie域的Google Chrome时遇到了问题(对于' localhost')。我删除了cookie中的域名,问题已经消失。

更多信息:Cookies on localhost with explicit domain

答案 1 :(得分:-1)

我将所有代码都删除到准系统中,看起来好像正在设置标题。也可能是因为我在Slip app配置中设置了域名。我还在/ etc / hosts /和vhost中添加了一个.com到我的域的末尾,以防它与Chrome在localhost上处理cookie有关。

相关问题