Skip to main content

PHP Create,Access,and Destroy Cookies Tutorial

create,access,and destroy cookies in php

Hi Guys,

In this blog,I will explain you how to create,access,and destroy cookies in php a cookie is a small file with the maximum size of 4KB that the web server stores on the client computer.They are typically used to keeping track of information such as a username that the site can retrieve to personalize the page when the utilizer visits the website next time.A cookie can only be read from the domain that it has been issued from.Cookies are conventionally set in an HTTP header but JavaScript can withal set a cookie directly on a browser.

Tip:Each time the browser requests a page to the server, all the data in the cookie is automatically sent to the server within the request.
Setting a Cookie in PHP

The setcookie() function is utilized to set a cookie in PHP. Ascertain you call the setcookie() function before any output engendered by your script otherwise cookie will not set. The basic syntax of this function can be given with:

Syntax:

setcookie(name, value, expire, path, domain, secure);

The parameters of the setcookie() function have the following meanings:

  1. Name:It is used to set the name of the cookie.
  2. Value:It is utilized to set the value of the cookie.
  3. Expire:It is utilized to set the expiry timestamp of the cookie after which the cookie can’t be accessed.
  4. Path:It is utilized to designate the path on the server for which the cookie will be available.
  5. Domain: It is used to specify the domain for which the cookie is available.
  6. Security:It is utilized to denote that the cookie should be sent only if a secure HTTPS connection exists.
Tip:

If the expiration time of the cookie is set to 0, or omitted, the cookie will expire at the end of the session i.e. when the browser closes.

Creating Cookies:

Here's an example that utilizes setcookie() function to engender a cookie named username and assign the value value Dharmik to it. It additionally specify that the cookie will expire after 30 days (30 days * 24 hours * 60 min * 60 sec).


<?php

// Setting a cookie
setcookie("username", "Dharmik", time()+30*24*60*60);

?>
Note:

Only the designation argument in the setcookie() function is obligatory.To skip an argument,the argument can be replaced by a vacuous string(“”).

Warning:

Don't store sensitive data in cookies since it could potentially be manipulated by the maleficent utilizer. To store the sensitive data securely use sessions instead.

Accessing Cookies Values

The PHP $_COOKIE superglobal variable is utilized to retrieve a cookie value. It typically an associative array that contains a list of all the cookies values sent by the browser in the current request, keyed by cookie name. The individual cookie value can be accessed utilizing standard array notation, for example to display the username cookie set in the precedent example, you could utilize the following code.

Example

<?php

// Accessing an individual cookie value
echo $_COOKIE["username"];

?>
Output

Dharmik

It's a good practice to check whether a cookie is set or not before accessing its value. To do this you can utilize the PHP isset() function, like this:

Example

<?php

// Verifying whether a cookie is set or not
if(isset($_COOKIE["username"])){
echo "Hi " . $_COOKIE["username"];
} else{
echo "Welcome MyWebtuts.com";
}

?>

You can utilize the print_r() function like print_r($_COOKIE); to optically discern the structure of this $_COOKIE associative array, like you with other arrays.

Removing Cookies

You can delete a cookie by calling the same setcookie() function with the cookie name and any value (such as a empty string) however this time you require the set the expiration date in the past, as shown in the example below:

Example

<?php

// Deleting a cookie
setcookie("username", "", time()-3600);

?>
Note:

The same path, domain, and other arguments should be passed that were used to engender the cookie in order to ascertain that the correct cookie is deleted.

It will help you..

Comments

Popular posts from this blog

Laravel Scout Algolia Full Text Search Example

Hi Guys Today,I will tell how you can full text search utilizing scout algolia. i will show the example of laravel scout algolia full text search.you can full text search utilizing scout algolia api.it can this example full expound scout algolia full text search. I will show the rudimental step of scout algolia api full text search.if you can utilize full text search for install scout and Algolia api package.we are utilizing algolia api utilizing full text search example in laravel. Here the following steps example laravel full text search Using scout algolia Step 1: Create Laravel Project In this step create laravel project following command. composer create-project --prefer-dist laravel/blog Step 2: Database Configuration After create laravel project , we require to make database configuration, you have to add following details on your .env file. 1.Database Username 1.Database Password 1.Database Name In .env file also available host and port details, you can configu

Laravel IP Address Using Get Location Example

Hi Dev, Today,I will learn you how to get location useing ip address in laravel. we will show example of laravel ip address using get location. you can easy to get location useing ip address in laravel.In this example, I will useing stevebauman/location packege get location useing ip address in laravel. Many time you will need to get visitor's details for security, spam prevention etc. It's very easy to get visitor ip address and their location in PHP Laravel. Step 1: Install stevebauman/location Now, We will install stevebauman/location package using below command.Require this package with composer. It is recommended to only require the package for development. composer require stevebauman/location Step 2: Add providers and aliases In this step,We will add below providers and aliases in the "config/app.php" file. config/app.php 'providers' => [ .... Stevebauman\Location\LocationServiceProvider::class, ], 'aliases' => [ .... 'Loca

Laravel 6 validation required if another field is empty

Hii guys, In this artical, i will give you example of laravel 6 in validation required if another field is empty. We know laravel provide several in-built laravel 6 validation required_without . If you need to add validation rules like required if other field is empty in laravel then you can do it using required_without. I am going to explain you, If you can not enter test (field) value at that time test1 (field) is required. So at that time you can add validation required_without. So, you can use this way: "test1" =>"required_without:test" Example: public function store(Request $request) { $request->validate([ "test" =>"required", "test1" =>"required_without:test" ]); dd("Done!"); } If return validation error otherwise show Done!. It will help you...