Matanya
Matanya

Reputation: 6346

Laravel 4 - Cookies aren't saved on local server

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

Upvotes: 0

Views: 2890

Answers (2)

Tanvir Gaus
Tanvir Gaus

Reputation: 195

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
}

Upvotes: 0

Antonio Frignani
Antonio Frignani

Reputation: 961

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.

Upvotes: 5

Related Questions