Home  >  Article  >  Backend Development  >  How to check if cookie is set in Laravel?

How to check if cookie is set in Laravel?

WBOY
WBOYforward
2023-09-12 13:49:01722browse

When you visit a web page, it usually generates text files containing small data such as usernames and passwords, and stores them on the user's browser. These are known cookies that are used to identify a user's system and can be accessed by the web server or the client computer (the computer where they are stored).

  • The information stored in cookies is specific to the web server.

  • Once you connect to the server, a cookie tagged with a unique ID is created and stored on your computer.

  • Once the cookie is exchanged/stored in the client, and if you connect to the server again, it will recognize your system based on the stored cookie.

  • This helps the server serve personalized pages to specific users.

Example 1

The following example creates a cookie and verifies that it is set.

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Cookie;

class UserController extends Controller {
   public function index(Request $request) {
      Cookie::queue('msg', 'cookie testing', 10);
      echo $value = $request->cookie('msg');
   }
}

Output

The output of the above code is -

How to check if cookie is set in Laravel?

Example 2

Another way to test whether the cookie is set can be seen in the example below -

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Cookie;

class UserController extends Controller {
   public function index(Request $request) {
      Cookie::queue('msg', 'cookie testing', 10);
      return view('test');
   }
}

Test.blade.php

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: 'Nunito', sans-serif;
      }
   </style>
<head>
<body class="antialiased">
   <div>
      {{ Cookie::get('msg') }}
   </div>
</body>
</html>

Output

The output of the above code is -

How to check if cookie is set in Laravel?

Example 3

Use the hasCookie() method to test whether a given cookie has been set.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Cookie;

class UserController extends Controller{
   public function index(Request $request) {
      if($request->hasCookie('msg')) {
         echo "Cookie present";
      } else {
         echo "Cookie msg is not set";
      }
   }
}

Output

Cookie present

Example 4

Another example of testing cookie settings.

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use Cookie;

class UserController extends Controller{
   public function index(Request $request) {
      return view('test');
  }
}

Test.blade.php

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: 'Nunito', sans-serif;
      }
   </style>
</head>
<body class="antialiased">
   <div>
      @if (Cookie::get('msg') !== false)
         <p>cookie is present.</p>
      @else
         <p>cookie is not set.</p>
      @endif
   </div>
</body>
</html>

Output

The output of the above code is -

cookie is present.

The above is the detailed content of How to check if cookie is set in Laravel?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete