0
0
Laravelframework~20 mins

Cookie handling in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cookie Mastery in Laravel
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Laravel cookie retrieval code?
Consider this Laravel controller method that tries to read a cookie named 'user_id'. What will be the output if the cookie does not exist?
Laravel
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CookieController extends Controller
{
    public function showUserId(Request $request)
    {
        $userId = $request->cookie('user_id', 'guest');
        return response()->json(['user_id' => $userId]);
    }
}
A{"user_id":null}
B{"user_id":""}
C{"user_id":"guest"}
DThrows an exception because cookie 'user_id' is missing
Attempts:
2 left
💡 Hint
Check the second parameter of the cookie() method in the Request object.
📝 Syntax
intermediate
2:00remaining
Which option correctly sets a cookie in Laravel with a 10-minute expiration?
You want to set a cookie named 'theme' with value 'dark' that expires in 10 minutes. Which code snippet correctly does this?
Areturn response('Set theme')->cookie('theme', 'dark', 10);
Breturn response('Set theme')->cookie('theme', 'dark', now()->addMinutes(10));
Creturn response('Set theme')->cookie('theme', 'dark', 600);
Dreturn response('Set theme')->cookie('theme', 'dark', '10m');
Attempts:
2 left
💡 Hint
The third parameter is the number of minutes until expiration.
🔧 Debug
advanced
2:00remaining
Why does this Laravel code fail to delete the cookie?
This code tries to delete a cookie named 'session_id' but the cookie remains in the browser. What is the reason?
Laravel
<?php

return response('Delete cookie')->cookie('session_id', '', -1);
ANegative expiration time is invalid and causes Laravel to ignore the cookie.
BThe cookie path or domain does not match the original cookie, so deletion fails.
CSetting the cookie value to empty string does not delete it; must use null instead.
DCookies cannot be deleted via response; must be deleted client-side with JavaScript.
Attempts:
2 left
💡 Hint
Cookies are deleted by setting expiration in the past but path and domain must match.
state_output
advanced
2:00remaining
What is the value of the cookie after this Laravel middleware runs?
This middleware adds a cookie named 'visited' with value 'yes' on the first request. What will be the cookie value on the second request?
Laravel
<?php

namespace App\Http\Middleware;

use Closure;

class CheckVisited
{
    public function handle($request, Closure $next)
    {
        if (!$request->cookie('visited')) {
            $response = $next($request);
            return $response->cookie('visited', 'yes', 60);
        }
        return $next($request);
    }
}
AOn second request, cookie 'visited' has value 'yes'.
BOn second request, cookie 'visited' is null because middleware does not set it again.
COn second request, middleware throws an error because cookie is missing.
DOn second request, cookie 'visited' has value 'no'.
Attempts:
2 left
💡 Hint
Cookies set in response are sent to browser and included in next request.
🧠 Conceptual
expert
2:00remaining
Which statement about Laravel cookie encryption is true?
Laravel encrypts cookies by default. Which of these statements correctly describes this behavior?
ACookies are encrypted only if the app is in production environment.
BCookies must be manually encrypted before setting them in Laravel responses.
CLaravel does not encrypt cookies; encryption must be handled by the developer.
DCookies are encrypted automatically unless added to the 'except' list in the EncryptCookies middleware.
Attempts:
2 left
💡 Hint
Check the EncryptCookies middleware behavior in Laravel.