What is cookie?
The cookie is a small file stored on a client computer through the webserver. In simple words, the server saved user information in the user computer as cookies.
A cookie is used to identify the user. It is part of the client's computer.
In the previous tutorial, we discussed the session in PHP. The session is part of the server but the cookie is part of the client computer browser.
The information stored in a web browser as cookies. Next time, when the browser sends the request on the server, it sends stored cookies information on the server and the server identifies the information. The size of the cookie file is the maximum 4KB that stored in the client computer.
Why need cookies?
The cookie is used to reduce efforts by the user. A file is created on the client computer.
For example -
The popup comes in the browser to save your login details. If you click on save, a file is created in the browser. Next time when the user logs in, the user does not need to enter login details and the browser sends saved details on the server after that the user gets logged in. Unless the cookie is deleted, the user gets logged in without entering login details.
That's why we need cookies
PHP creating cookies
Setting cookie
In PHP , we use setcookie() function.The function setcookie() helps to create cookie.
Syntax –
setcookie(name, value, expire, path, domain, security,httponly);
Name- The name of the cookie. It's mandatory.The server will use when retrieving its value from the $_COOKIE array variable
Value -The setcookie() function set the value of the name variable and It contains that you actually want to store. It's also mandatory.
Expiry- This is a limit to access a cookie. The time may be 1 day ,1 month or more.
If you set the time 1 month then you can access the cookie during the 1-month duration. After one month you can not access the cookie.
Path- Path specifies the directories from which the cookie is valid .
Domain - This can be used to specify the domain name in very large domains and
must contain at least two periods to be valid. All cookies are only valid for the host
and domain which created them.
Security- It set to be 1 refers that the cookie should only be sent by secure transmission using HTTPS and else set to 0 which means cookie can be sent by regular HTTP.
httponly
If it is set to true, then only client side scripting languages .for example - JavaScript cannot access them.
Set the cookie username, mobile, and email. The cookie will expire after one hour in the below example.
setcookie() function
<?php
setcookie("username", "john", time()+3600, "/","", 0);
setcookie("email", "[email protected]", time()+3600, "/", "", 0);
?>
Retrieving the Cookie value with PHP
Create cookies and define the values. After this process , you can retrieve cookies value on page reload.
Let's have a look and try to page reload.
Create a file page_name.php and try it.
<?php
$name= "admin";
$name_value= "saim";
setcookie($name, $name_value, time() +3600, "/"); //1 hours
?>
<?php
if(!isset($_COOKIE[$name])) {
echo "Cookie named '" . $name . "' is not set!";
} else {
echo "Cookie '" . $name . "' is set!
";
echo "Value is: " . $_COOKIE[$name]."
";
}
echo "
";
echo "Reload the page to see the cookie value. The cookie will expire in 1 hours";
?>
Outupt
After reload page output .
Cookie 'admin' is set!
Value is: saim
Modfying cookie value using PHP
The modification of the cookie value is very easy. Its just part of the edit operation.
Change vale saim to jack and try again the same file.
<?php
$name= "admin";
$name_value= "jack";
setcookie($name, $name_value, time() +3600, "/"); //1 hours
?>
<?php
if(!isset($_COOKIE[$name])) {
echo "Cookie named '" . $name . "' is not set!";
} else {
echo "Cookie '" . $name . "' is set!
";
echo "Value is: " . $_COOKIE[$name]."
";
echo "The cookie will expire in 1 hours";
}
?>
Outupt
Change and reload the page.
Cookie 'admin' is set!
Value is: jack
How to delete a cookie in PHP
To delete a cookie you need to use setcookie() function as well as with its name.
You can destroy cookies before the expiry time. You need to define your given expiry time.
Let's have a look.
The PHP code help destroy the cookies before the expiry time.
Example
<?php
setcookie("admin", "", time() - 3600,'/');
echo "cookie is deleted";
?>
Conclusion
As we discussed above, we use the cookie to reduce efforts.
The Cookie is a small file to save user data and next time the user does not need to enter that information. That's why it reduces the efforts.
Set expiry time. Do not waste your time to put the same information from time to time. Create the cookie and set expiry time.
Restriction for the unknown user- We can delete the cookie before the expiry time. It's made restrictions for the unknown user. An unknown user never sees the authorized information.
The cookie is saved on the client computer(browser).