How to set php cookie with example in 2022

How to set php cookie with example in 2021
How to set PHP cookie with example in 2021

How to set PHP cookie with example in 2022

A cookie in PHP: Cookies are small pieces of data that are stored on your computer or any other device that can access the internet. You can use them to store information about your browsing activities and preferences.

The name "cookie" comes from the word "magic cookie."

When you press a button, it will store some data in the form of a cookie. Those cookies are very helpful when you want to save some information on your browser for later use. 

For example, if you type in the website address on the address bar but don't press on entering, it will automatically remember what you typed in case you decide to go back later and finish typing out the website address. 

The PHP cookie is a PHP runtime that is used by most websites today including Facebook, Twitter, and Google+. Cookies

A cookie in PHP: Cookies are used to store data or information in a variable that can be used anywhere in the project. Cookies variables expire after the defined time interval. 

A cookie is a file that the server embeds on the user's computer. We can create and retrieve cookie values. setcookie() is a function that is used to set a cookie.

Syntax of setcookie() function in PHP

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

name: It is the name of the cookie variable. This variable is used to access cookie data.

value: It is the value of the variable.

expire: This parameter specifies a particular time in seconds. After this time cookie will expire and become inaccessible. If this parameter is not set then the cookie will automatically expire when the Web Browser is closed.

path: path specifies the directories for which the cookie is valid. A single forward-slash(/) permits the cookie to be valid for all directories.

domain: It is the Domain name in which the cookies will work.

secure: It is a transmission type that should be either 1 or 0. if 1 means cookies will be sent by HTTPS(Secure transmission). if 0 means cookies will be sent by HTTP(Regular transmission).

How to Creating Cookies with PHP?

first.php

<?php

setcookie("name", "Icoderweb", time()+60, "/","", 0);

setcookie("marks", "380", time()+60, "/", "", 0);

setcookie("course", "MCA", time()+60, "/", "", 0);

?>

Accessing Cookies With PHP: $_COOKIE is a super global variable which is used to access cookies data.

How to accessing Cookies with PHP?

second.php

<?php

echo "Student Details<br>";

echo "Student Name:".$_COOKIE['name']."<br>";

echo " Student Marks:".$_COOKIE['rollno']."<br>";

echo " Student Course:".$_COOKIE['branch']."<br>";

?>

*****OUTPUT*****

Student Details

Student Name: Icoderweb

Student Marks:380

Student Course: MCA

Note: First run first.php then run second.php to get the result.

Deleting a Cookie: setcookie() function is also used to delete cookies.

How to delete Cookies with PHP?

First.php

<?php

setcookie("name", "", time()-60, "/","", 0);

setcookie("marks", "", time()-60, "/", "", 0);

setcookie("course", "", time()-60, "/", "", 0);

?> 

Checking Cookies are active or not: isset() is a predefined function whcih is used to check cookies are set or not.

How to checking Cookies are active or not with PHP?

Second.php

<?php

if(isset($_COOKIE["name"])||isset($_COOKIE["marks"])||isset($_COOKIE["course"]))

{

echo "Student Name:" . $_COOKIE["name"] . "<br />";       

echo "Student Marks:" . $_COOKIE["marks"] . "<br />";     

echo "Student Course:" . $_COOKIE["course"] . "<br />"; }

else

echo "Cookies are not active" . "<br />";

?>

*****OUTPUT*****

Cookies are not active

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.