Laravel Cookie | Laravel Cookie
What is Laravel Cookie?
- A cookie is information that a Web site puts on your hard disk so that it can remember something about you at a later time. (More technically, it is information for future use that is stored by the server on the client side of a client/server communication.) Typically, a cookie records your preferences when using a particular site.
- Using the Web's Hypertext Transfer Protocol (HTTP), each request for a Web page is independent of all other requests. For this reason, the Web page server has no memory of what pages it has sent to a user previously or anything about your previous visits.
- A cookie is a mechanism that allows the server to store its own information about a user on the user's own computer.
- You can view the cookies that have been stored on your hard disk (although the content stored in each cookie may not make much sense to you).
- The location of the cookies depends on the browser.
- Internet Explorer stores each cookie as a separate file under a Windows subdirectory.
- Netscape stores all cookies in a single cookies.txt fle. Opera stores them in a single cookies.dat file.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
Creating Cookie:
- Cookie can be created by global cookie helper of Laravel.
- Itis an instance of Symfony\Component\HttpFoundation\Cookie.
- The cookie can be attached to the response using the withCookie() method.
- Create a response instance of Illuminate\Http\Response class to call the withCookie() method.
- Cookie generated by the Laravel are encrypted and signed and it can’t be modified or read by the client.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts
Here is a sample code with explanation.
Cookie() method will take 3 arguments.
- Cookie can be created by global cookie helper of Laravel. Itis an instance of Symfony\Component\HttpFoundation\Cookie. The cookie can be attached to the response using the withCookie() method. Create a response instance of Illuminate\Http\Response class to call the withCookie() method. Cookie generated by the Laravel are encrypted and signed and it can’t be modified or read by the client.
- First argument is the name of the cookie,
- second argument is the value of the cookie
- third argument is the duration of the cookie after which the cookie will get deleted automatically.
Cookie can be set forever by using the forever method as shown in the below code
Retrieving Cookie:
- Once we set the cookie, we can retrieve the cookie by cookie() method.
- This cookie () method will take only one argument which will be the name of the cookie.
- The cookie method can be called by using the instance of Illuminate\Http\Request.
Here is a sample code.
Example:
- Step 1− Execute the below command to create a controller in which we will manipulate the cookie.
- Step 2 − After successful execution, you will receive the following output −
- Step3 −Copy the following code in app/Http/Controllers/CookieController.php file.
- Step 4 − Add the following line in app/Http/routes.php file.
- Step 5 − Visit the following URL to set the cookie.
- http://localhost:8000/cookie/set
- Step 6 − The output will appear as shown below.
- The window appearing in the screenshot is taken from firefox but depending on your browser, cookie can also be checked from the cookie option.
- Step 7 − Visit the following URL to get the cookie from the above URL.
- http://localhost:8000/cookie/get
- Step 8 − The output will appear as shown in the following image.