How to check if cookie is set in Laravel?

WBOY
Release: 2023-09-12 13:54:01
forward
729 people have browsed it

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');
   }
}
Copy after login

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');
   }
}
Copy after login

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>
Copy after login

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";
      }
   }
}
Copy after login

Output

Cookie present
Copy after login

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');
  }
}
Copy after login

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>
Copy after login

Output

The output of the above code is -

cookie is present.
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!