Laravel Basic
Laravel Form
Laravel Database
Laravel Advance
Laravel makes cookie handling easy and secure by automatically encrypting them.
✅ Store small user-specific data on the client side.
✅ Make the app remember user settings/preferences.
✅ Maintain login state (like "Remember Me").
✅ Track activity across pages.
⚠️ Best for lightweight data (not for sensitive large data).
EncryptCookies
(enabled by default) handles encryption/decryption.AddQueuedCookiesToResponse
attaches cookies to responses.web.php (routes\web.php)
Route::get('/cookies_store', function () { Cookie::queue('user_name', 'john', 120); // 120 mins return "Cookies Stored"; }); Route::get('/cookies_get', function () { return Cookie::get('user_name'); }); Route::get('/cookies_clear', function () { Cookie::queue(Cookie::forget('user_name')); return "Cookies Removed"; });
Output Store Cookies:
Output Get Cookies:
Output Remove Cookies:
Route::get('/set-cookie', function () { // name, value, minutes return response('Cookie set successfully') ->cookie('username', 'John', 60); // 60 mins });
OR
use Illuminate\Support\Facades\Cookie; Route::get('/set-facade', function () { Cookie::queue('language', 'English', 120); // 120 mins return "Cookie set using facade!"; });
Route::get('/get-cookie', function (Illuminate\Http\Request $request) { $name = $request->cookie('username'); return "Cookie Value: " . $name; });
OR
use Illuminate\Support\Facades\Cookie; Route::get('/set-facade', function () { $name = Cookie::get('username'); return "Cookie Value: " . $name; });
use Illuminate\Support\Facades\Cookie; Route::get('/delete-cookie', function () { Cookie::queue(Cookie::forget('username')); return "Cookie Deleted!"; });
Cookie::queue('theme', 'dark', 1440); // 1 day (60*24 minutes)
document.cookie
)return response('Secure Cookie') ->cookie('secure_token', '12345', 60, '/', null, true, true);
Feature |
Cookies |
Sessions |
---|---|---|
Storage |
Client (Browser) |
Server |
Security |
Less secure (stored in client) |
More secure (stored in server) |
Size Limit |
~4KB per cookie |
No fixed size (depends on server) |
Lifetime |
Until expired or deleted |
Until user logs out/session ends |
Use Case |
Remember user preference, tracking |
User authentication, shopping cart |