How To Set Cookies In Php
A cookie in PHP is a modest file with a maximum size of 4KB that the web server stores on the customer computer. They are typically used to keep track of information such as a username that the site can retrieve to personalize the page when the user visits the website adjacent time. A cookie can only exist read from the domain that information technology has been issued from. Cookies are usually set up in an HTTP header but JavaScript tin as well set a cookie straight on a browser.
Setting Cookie In PHP: To prepare a cookie in PHP, the setcookie() part is used. The setcookie() function needs to be chosen prior to whatsoever output generated by the script otherwise the cookie volition not be set.
Syntax:
setcookie(name, value, elapse, path, domain, security);
Parameters: The setcookie() function requires half-dozen arguments in full general which are:
- Name: It is used to set the name of the cookie.
- Value: It is used to set the value of the cookie.
- Expire: It is used to set the expiry timestamp of the cookie after which the cookie can't exist accessed.
- Path: It is used to specify the path on the server for which the cookie will be available.
- Domain: It is used to specify the domain for which the cookie is bachelor.
- Security: It is used to indicate that the cookie should be sent only if a secure HTTPS connexion exists.
Beneath are some operations that can be performed on Cookies in PHP:
- Creating Cookies: Creating a cookie named Auction_Item and assigning the value Luxury Car to it. The cookie volition expire after two days(2 days * 24 hours * 60 mins * 60 seconds).
Example: This instance describes the cosmos of the cookie in PHP.
PHP
<!DOCTYPE html>
<?php
setcookie(
"Auction_Item"
,
"Luxury Car"
, fourth dimension() + two * 24 * 60 * 60);
?>
<html>
<body>
<?php
repeat
"cookie is created."
?>
<p>
<potent>Note:</stiff>
Yous might accept to reload the
page to run across the value of the cookie.
</p>
</body>
</html>
Note: Only the name argument in the setcookie() function is mandatory. To skip an argument, the argument can be replaced by an empty string("").
Output:
Checking Whether a Cookie Is Ready Or Not: It is ever appropriate to check whether a cookie is set or non before accessing its value. Therefore to check whether a cookie is set or not, the PHP isset() role is used. To bank check whether a cookie "Auction_Item" is set or not, the isset() role is executed as follows:
Instance: This example describes checking whether the cookie is set or not.
PHP
<!DOCTYPE html>
<?php
setcookie(
"Auction_Item"
,
"Luxury Machine"
, time() + ii * 24 * 60 * sixty);
?>
<html>
<body>
<?php
if
(isset(
$_COOKIE
[
"Auction_Item"
]))
{
echo
"Auction Item is a "
.
$_COOKIE
[
"Auction_Item"
];
}
else
{
repeat
"No items for auction."
;
}
?>
<p>
<strong>Note:</strong>
You might accept to reload the page
to see the value of the cookie.
</p>
</body>
</html>
Output:
Accessing Cookie Values: For accessing a cookie value, the PHP $_COOKIE superglobal variable is used. It is an associative array that contains a record of all the cookies values sent by the browser in the current request. The records are stored as a list where the cookie name is used as the key. To admission a cookie named "Auction_Item", the following code can be executed.
Example: This case describes accessing & modifying the cookie value.
PHP
<!DOCTYPE html>
<?php
setcookie(
"Auction_Item"
,
"Luxury Car"
, fourth dimension() + 2 * 24 * sixty * 60);
?>
<html>
<torso>
<?php
echo
"Sale Item is a "
.
$_COOKIE
[
"Auction_Item"
];
?>
<p>
<strong>Notation:</strong>
Yous might have to reload the folio
to run into the value of the cookie.
</p>
</body>
</html>
Output:
Deleting Cookies: The setcookie() office can be used to delete a cookie. For deleting a cookie, the setcookie() part is called by passing the cookie proper noun and other arguments or empty strings but even so this time, the expiration date is required to be set in the past. To delete a cookie named "Auction_Item", the post-obit code tin be executed.
Case: This example describes the deletion of the cookie value.
PHP
<!DOCTYPE html>
<?php
setcookie(
"Auction_Item"
,
"Luxury Car"
, time() + 2 * 24 * lx * 60);
?>
<html>
<body>
<?php
setcookie(
"Auction_Item"
,
""
, time() - 60);
?>
<?php
echo
"cookie is deleted"
?>
<p>
<strong>Annotation:</strong>
You might take to reload the folio
to see the value of the cookie.
</p>
</trunk>
</html>
Output:
Important Points:
- If the expiration time of the cookie is set to 0 or omitted, the cookie volition expire at the finish of the session i.e. when the browser closes.
- The same path, domain, and other arguments should exist passed that were used to create the cookie in social club to ensure that the correct cookie is deleted.
PHP is a server-side scripting language designed specifically for web development. You tin can learn PHP from the footing up by following this PHP Tutorial and PHP Examples.
How To Set Cookies In Php,
Source: https://www.geeksforgeeks.org/php-cookies/
Posted by: moorejusbut1962.blogspot.com
0 Response to "How To Set Cookies In Php"
Post a Comment