Laravel Cookie

What is a Cookie?

  • A cookie is a small piece of data stored on the client’s browser.
  • In Laravel, cookies are often used to store temporary information like:
  • User preferences (theme, language, etc.)
  • Session identifiers
  • Remember-me tokens

Laravel makes cookie handling easy and secure by automatically encrypting them.


Why Use Cookies?

✅ 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).


How Laravel Handles Cookies

  • By default, Laravel cookies are encrypted & signed (so users can’t tamper with them).
  • Middleware EncryptCookies (enabled by default) handles encryption/decryption.
  • Middleware AddQueuedCookiesToResponse attaches cookies to responses.

Basic Cookies

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:


How to Use Cookies in Laravel

1. Store Data in 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!";
});


2. Get Data from Cookies

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


3. Remove Data from Cookies

use Illuminate\Support\Facades\Cookie;

Route::get('/delete-cookie', function () {
  Cookie::queue(Cookie::forget('username'));
  return "Cookie Deleted!";
});


4.Cookie Lifetime

  • Default lifetime = till the browser is closed (session cookie).
  • To make persistent:
Cookie::queue('theme', 'dark', 1440); // 1 day (60*24 minutes)


5. Secure & HttpOnly Cookies

  • Secure → Only sent over HTTPS
  • HttpOnly → Not accessible via JavaScript (document.cookie)
return response('Secure Cookie')
    ->cookie('secure_token', '12345', 60, '/', null, true, true);

Difference Between Cookies & Sessions

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


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.