How to create and destroy cookie in Laravel 5.7?

How to create and destroy cookie in Laravel 5.7?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Managing Cookies and Sessions in Laravel 5.7

Laravel provides a clean, expressive facade for interacting with HTTP headers and server-side storage. While the underlying mechanisms rely on standard PHP functions like setcookie() or session array manipulation, using the Cookie and Session facades is the idiomatic approach. This abstraction handles expiration dates, pathing, domain restrictions, and encryption automatically, reducing the risk of security vulnerabilities associated with manual header management.

Creating Cookies

To persist data across requests without relying on the session state, you create a cookie using the Cookie::queue() method within your controller or service provider. This queues the operation to be applied in the final response. You define the key, value, expiration time, and optional security flags like secure (HTTPS only) and httpOnly (prevents JavaScript access).

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;

public function login(Request $request)
{
    // ... authentication logic ...

    if ($request->user()) {
        // Set a cookie that expires in 7 days
        Cookie::queue(
            'remember_token', 
            $request->user()->token, 
            604800
        );

        return redirect('/dashboard');
    }
}

The secure flag is critical for production environments. If your application runs over HTTPS, setting this flag ensures the cookie is never transmitted over insecure HTTP connections, mitigating man-in-the-middle attacks. Similarly, the httpOnly attribute prevents cross-site scripting (XSS) attacks from stealing session or cookie data via JavaScript.

Destroying Cookies

Laravel does not provide a direct "delete" method for cookies in the same way it handles sessions, but the standard practice involves setting an expiration date that has already passed. By queueing a new cookie with the exact same name and a negative expiration time (e.g., 0 seconds), you instruct the browser to immediately remove the stored value.

public function logout()
{
    // Clear all queued cookies before redirecting
    Cookie::queue(
        'remember_token', 
        null, // Setting value to null effectively clears it
        -1    // Negative expiration time ensures immediate deletion
    );

    return redirect('/login');
}

When using Cookie::forget('key'), Laravel internally executes the logic to set the cookie with a zero or negative expiry. This pattern is preferred over trying to access raw headers because it ensures the response header is modified correctly before the output buffer is flushed.

Working with Sessions

Sessions in Laravel differ from cookies because they rely on server-side storage (files, database, or Redis) rather than client-side persistence. By default, Laravel 5.7 stores sessions as serialized files on the disk. This approach keeps sensitive data like authentication tokens and user preferences off the client machine entirely.

You can manipulate session data directly using the Session facade. Data stored here is available automatically in Blade templates via the {{ session('key') }} syntax.

use Illuminate\Support\Facades\Session;

public function storeItem($itemId)
{
    // Store a value in the session for 60 minutes
    Session::put('cart_item', $itemId);

    return redirect('/checkout');
}

Sessions are particularly useful for maintaining state during multi-step processes, such as a checkout flow or a wizard. Unlike cookies, you do not need to worry about client storage limits (which are typically 4KB). However, sessions introduce latency because the server must read and write to storage on every request. If your application scales horizontally, you must ensure all servers share the same session store, such as Redis, otherwise users will see different data depending on which server handles their request.

Trade-offs and Best Practices

While cookies are perfect for non-sensitive data like language preferences or theme settings, they should generally avoid storing authentication tokens if possible. Storing sensitive data in a cookie increases the attack surface; if an attacker manages to steal the cookie (via XSS), they gain immediate access.

For high-traffic applications, file-based sessions can become a bottleneck. If you anticipate significant load, consider switching your session driver to database, redis, or memcached by modifying the configuration in config/session.php. This decouples the application's performance from the disk I/O speed of individual servers.

Finally, always remember that Laravel handles the HTTP response headers for you. You do not need to manually call header() after setting a cookie or session value. The framework ensures these changes are applied to the outgoing response consistently, preventing race conditions where headers might be sent before the cookie logic executes.

Our network