0

Following the docs I have created a cookie to last "forever" (5 years) on my local virtual host (indgo.dev).

so this:

 $cookie = Cookie::forever('hash',$project['hash']);
  dd($cookie);

outputs:

object(Symfony\Component\HttpFoundation\Cookie)#436 (7) {
  ["name":protected]=>
  string(4) "hash"
  ["value":protected]=>
  string(360) "eyJpdiI6IlNJcUJZSElRTlwvQ0dJU3Z4dE44VFwvYjJ3U3lpckRZY2xsV3NlWTJ5VHJ1dz0iLCJ2YWx1ZSI6IkFHeFJGOXhjSzZxTkhZWGpIMiszUWZ5eXBUT2xuMTZFalpXdVZ3VW1CYUh1SmxKZUNPMk1rSFhONFk4REVkMzBtWlluWVhWU21uVHJXMDllKytmYm5idk5IVTNcLzIrUEgyZ3dsVllVTERyeTROU1lKUHUwb1ZpRll2V1JmU0Z4bSIsIm1hYyI6Ijc1YzcyODlkOTU0MGQ3ZjEyMDJhNjk5ZWNhOWY2ZWNhMGRhNzU4NjZiOTAwNGUzMjY1MzI2YjhhNGZjMWVhMzgifQ=="
  ["domain":protected]=>
  NULL
  ["expire":protected]=>
  int(1523085636)
  ["path":protected]=>
  string(1) "/"
  ["secure":protected]=>
  bool(false)
  ["httpOnly":protected]=>
  bool(true)
}

However, when I try to get the cookie on a differernt request:

$hash = Cookie::has('hash');
    dd($hash);

I get false (or null if I use the get method)

Using Chrome Dev tools I found that indeed the cookie doesn't show. The only ones listed are laravel_payload and laravel_session.

UPDATE: When I login a new remember_me cookie is created by the Auth class

Phill Sparks
  • 20,000
  • 3
  • 33
  • 46
Matanya
  • 6,233
  • 9
  • 47
  • 80
  • Try using Cookie::get('hash'). Maybe L4 has slightly different impelemntation of Cookies, its still not documented on four.laravel.com. – Marko Aleksić Apr 08 '13 at 07:38
  • actually it is: http://four.laravel.com/docs/requests#cookies. And as I said I already tried the `get` method as well – Matanya Apr 08 '13 at 07:41
  • already answered by me here: [link](http://stackoverflow.com/questions/15777824/cant-set-cookies-in-laravel-4/) – Antonio Frignani Apr 08 '13 at 15:07
  • I read your answer b4 I posted my question. I **am** trying to get the cookie on a different request – Matanya Apr 08 '13 at 15:13

2 Answers2

5

You are not sending the cookie in the response. Try

$cookie = Cookie::forever('hash',$project['hash']);
return Response::make()->withCookie($cookie);

on the first request, then try to get the cookie.

Antonio Frignani
  • 961
  • 1
  • 11
  • 23
  • Thanks. It works. How would I go about sending a view as a Response along with the cookie? – Matanya Apr 08 '13 at 17:39
  • 3
    You can send a view to a response with return Response::make(View::make('hello')); and can attach a cookie to it with return Response::make(View::make('hello'))->withCookie($cookie); – Antonio Frignani Apr 09 '13 at 07:36
  • I tried something similar to your example and I couldn't fetch the cookie in any request after it was set. The cookie was there but it was impossible to read it. Until I figure out that Laravel doesn't allow `.` (dot) in cookie name. Actually it stores the cookie, but doesn't get it later. – Gustavo Straube Aug 12 '15 at 20:43
  • The dot is probably used to get a value from an array. So if you set your cookie like ```Cookie::forever('hash', ['first' => 1]);``` you will be able to fetch it via ```Cookie::get('hash.first');``` – Antonio Frignani Aug 24 '15 at 18:53
0

I was having the issue with the cookie. Just in case the Laravel 4 newbies, if you need to set the cookie you need to send the cookie to any response. in my case I was showing a splash screen and need to set & check that when the user comes to the site. The splash screen has something like the following:

if(Input::get('splash') || Cookie::get('splash')) {
    $cookie = Cookie::forever('splash', 'checked');
    return Redirect::route('home')->withCookie($cookie);
} else {
    //show the splash screen with the form which has the splash = 1 as a hidden input
}
Tanvir Gaus
  • 205
  • 1
  • 3
  • 10