Today we will learn about PHP Cookie. The server embeds a small file known as a PHP Cookie on the user’s computer. Before these, we also discussed Form In PHP , Registration Form Using form Tag , PHP – Loops | for-loop, while-loop, do-while loop PHP Control Structures: If else, Switch Case, Data Type in PHP:, Operators In PHP, if you don’t know then Hurry up Now!
Here’s The Secret Sauce for Success in PHP Cookie?
On the user’s computer, a cookie is a little text file. A cookie can only contain files up to 4KB in size. Additionally, it goes by the names HTTP cookie, web cookie, and internet cookie. A website that a user visits for the first time sends data packets in the form of cookies to the user’s computer.
The majority of websites on the internet feature aspects from other fields, such as advertising. The domains hosting these components have the ability to establish their own cookies. These are referred to as third-party cookies.
A user can only view cookies they have created themselves. Its value is invisible to other users.
The majority of online browsers have the ability to disable both first-party and third-party cookies.
If so, PHP reacts by including the cookie token into the URL.
Types Of PHP Cookie
Session Cookie: Session Cookie, Temporary cookies of this kind expire when the browser is closed or the session ends.
Persistent Cookie: A cookie needs to have an expiration date in order to be persistent. The cookie will then remain valid until the specified expiration time, at which point it will expire.
PHP Cookie Creation
we can create cookies by setcookie() function.
NOTE: The <html> tag must come AFTER the setcookie() function.
Syntax:
setcookie(name, value, expire, path, domain, secure, httponly);
Only the name parameter is necessary. All additional criteria are optional.
HERE,
- The PHP function called “setcookie” is used to create the cookie.
- The name of the cookie that the server will use to get its value out of the $_COOKIE array variable is “cookie name.” It is required.
- “cookie value” is the cookie’s value and a required term.
- “[expiry time]” is an optional field that can be used to specify the cookie’s expiration time, such as one hour. The time is set using the time() function in PHP plus or minus a number of seconds greater than 0, for example, time() + 3600 for an hour.
- It is possible to set the cookie path on the server using the optional parameter “[cookie path]”. The cookie will be made accessible across the entire domain when the forward slash “/” is used. Subdirectories only allow the subdomain to access cookies.
- It is optional to use “[domain]” to specify the cookie access hierarchy; for example, www.cookiedomain.com means the entire domain, whereas www.sub.cookiedomain.com restricts cookie access to www.sub.cookiedomain.com and its subdomains. Keep in mind that you are allowed to have a subdomain within a subdomain as long as the total number of characters does not go over 253.
- “[secure]” is optional and is set to false by default. If set to true, it determines whether the cookie is sent via HTTPS or, if set to false, whether it is sent via HTTP.
- “[Httponly]” is not required. Only client-side scripting languages, such as JavaScript, will be unable to access them if it is set to true.
PHP Create/Retrieve a Cookie
We’ll write a simple program that will allow us to save the username in a cookie that will expire in ten seconds.
The implementation of “cookies.php,” the preceding example, is shown in the code below.
<?php
setcookie("user_name", "GeektoCode", time()+ 10,'/'); // expires after 10 sec
echo 'the cookie has been set for 10 seconds';
?>
OUTPUT
the cookie has been set for 10 seconds
EXAMPLE 2:
Using the global variable $_COOKIE, we then get the value of the cookie “user.” The isset() function is also used to determine whether the cookie is set:
<?php
setcookie("user", "GeektoCode");
?>
<html>
<body>
<?php
if(!isset($_COOKIE["user"])) {
echo "Sorry, We cannot find any Cookie!";
} else {
echo "<br/>Cookie Value: " . $_COOKIE["user"];
}
?>
</body>
</html>
OUTPUT:
Sorry, We cannot find any Cookie!
Cookie is not initially set. However, if you reload the page, you will see that the cookie has been set.
OUTPUT:
Cookie Value: GeektoCode
Remember that, Simply use the setcookie() function to set the cookie once more to modify it:
Delete a PHP Cookie
Using the setcookie() function and past expiration date will allow you to delete cookies:
<?php
// set the expiration date to one hour ago
setcookie("username", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'username' is deleted.";
?>
</body>
</html>
OUTPUT:
Cookie ‘username’ is deleted.