Cookies Intro – Storing Cookies in PHP Using setcookie() method

0
514
Understaing Cookies in PHP - How to Store Cookies
Understaing Cookies in PHP - How to Store Cookies

Here’s a basic PHP program for beginners to learn the concept of using Cookies in PHP. This is an introductory tutorial to Storing Cookies in PHP so don’t get carried away with all the tough stuff. We will cover each and everything of Cookies in upcoming articles, till then learn the basics and practice the given example programs.

Cookies in PHP – Definition

A cookie is a small text file stored by a server on the clients’ machine through the web browser. It can store alphanumeric text and it can be read only from the issuing domain.

THIRD PARTY COOKIES:
Multiple elements on a web page can be embedded from multiple domains, each of which can issue, its own cookie. These are known as Third Party Cookies. To make things clear, suppose I have a web page, on the page, I have embedded a youtube video. Now if that youtube video element issues cookies, it will be a third party cookie.

Why do we need cookies? – Understanding Cookies in PHP

Http is a stateless protocol which means that once web server completes a client request for a web page, the connection between the two goes away. In other words, there is no way for a server to identify that a sequence of all request originated from the same client.

There are many ways to keep the track of State Information between request (also known as SESSION TRACKING)

-Use of Hidden Fields
-URL Rewriting
-Cookies

So basically we use Cookies for Session Tracking.

setcookie() method – How to Set Cookies in PHP?

The method used to store and delete a cookie is known as setcookie() and it has 7 attributes. The syntax for setcookie() method is:

setcookie(name, value, duration, patch, domain, secure, http)
-The first two attributes are mandatory and rest can be given or not, depends on you.

 

An Example Program to Store Cookies in PHP using setcookie() method

cookies in php Example Program
cookies in php Example Program
<?php 
setcookie("user","Macky",time()+86400);
?>
<html><body>
<?php
if(!isset($_COOKIE["user"])){
 echo "Cookie is not set";
}
else
{
 echo "Cookie Value = ". $_COOKIE['user'];
}
 ?>
</body>
</html>

See the example code. We used setcookie() method and assigned value ‘macky’ to ‘user’ and for duration, we gave time()+86400.

{The time() function will return a total number of seconds from 1st January till today. 1 day = 86400 Seconds, so we added that to our time(). Which means we are telling that store this set of cookie for the next 86400 Seconds or 1 day}.

I hope you liked this article and found it helpful in ways. I will post a couple of more articles on Cookies in PHP and very soon will be posting a Student Management System Project In PHP for beginners so stick with us. You can join us on our Facebook or subscribe to the newsletters to get all the updates.

LEAVE A REPLY

Please enter your comment!
Please enter your name here