0

I'm new to the Slim framework. I'm trying to use the middleware functionality to do some authentication. However, Although I can set a cookie on one route, it disappears on a page refresh.

       $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();

Doing the print_r shows that the cookie has been set

[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 ) )

Or at least setCookie is picking up the value. However, if I then go to my other route

     $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());
          }
          });

Then the cookies are not in there. Also in the logs of my firebug console there is no cookie showing up which I have set.

Any idea what the problem might be? I've found a couple of posts online like http://help.slimframework.com/discussions/problems/652-cant-seem-to-get-php-cookies-working

but I can't figure out the problem so far. I'm running MAMP and I've got a vhost set up for the domain.

Mcrypt php extension is enabled.

Any ideas?

weaveoftheride
  • 4,092
  • 8
  • 35
  • 53

2 Answers2

0

I've had problems using Google Chrome with the cookie domain (for 'localhost'). I deleted the domain in cookies and problems have disappeared.

More info: Cookies on localhost with explicit domain

Community
  • 1
  • 1
Javier Gil
  • 58
  • 6
-1

I stripped out all of my code to the barebones, and it looked like something was setting the headers. Also it might have been because I set the domain in the Slip app config. I also added a .com to the end of my domains in /etc/hosts/ and in the vhost in case it was to do with Chrome's handling of cookies on localhost.

weaveoftheride
  • 4,092
  • 8
  • 35
  • 53