0% found this document useful (0 votes)
28 views12 pages

Cookies

The document explains the concept of cookies in web development, detailing how they are used to identify users and how to create, retrieve, modify, and delete cookies using PHP. It provides code examples for each operation, emphasizing the importance of refreshing the page to see changes in cookie values. Additionally, it notes that the setcookie() function must be called before any HTML output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views12 pages

Cookies

The document explains the concept of cookies in web development, detailing how they are used to identify users and how to create, retrieve, modify, and delete cookies using PHP. It provides code examples for each operation, emphasizing the importance of refreshing the page to see changes in cookie values. Additionally, it notes that the setcookie() function must be called before any HTML output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CCCS - 415

Advanced Web Development Using


What is a Cookie?
• A cookie is often used to identify a user.
• A cookie is a small file that the server embeds
on the user's computer.
• Each time the same computer requests a page
with a browser, it will send the cookie too.
• With PHP, you can both create and retrieve
cookie values.
How Cookies Work ?
How Cookies Works ?
Create Cookies With PHP
• A cookie is created with
the setcookie() function.
– Syntax
• setcookie(name, value, expire, path, domain, secure,
httponly);

• Only the name parameter is required. All other


parameters are optional.
PHP Create/Retrieve a Cookie
• The following example creates a cookie named
"user" with the value "John Doe".
• The cookie will expire after 30 days (86400 * 30).
• The "/" means that the cookie is available in entire
website (otherwise, select the directory you prefer).
• We then retrieve the value of the cookie "user"
(using the global variable $_COOKIE).
• We also use the isset()function to find out if the
cookie is set:
<!DOCTYPE html>
<?php
PHP Create/Retrieve a
$cookie_name = "user";
$cookie_value = "John Doe";
Cookie Example
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

<p><strong>Note:</strong> You might have to reload the page to see the value of the
cookie.</p>

</body> Note: The setcookie() function must


</html> appear BEFORE the <html> tag.
Modify a Cookie Value
• To modify a cookie, just set (again) the cookie
using the setcookie() function :
<!DOCTYPE html>
<?php Modify a
$cookie_name = "user";
$cookie_value = "Alex Porter"; Cookie Value
setcookie($cookie_name, $cookie_value, time() +
(86400 * 30), "/");
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>

<p><strong>Note:</strong> You might have to reload the page to


see the new value of the cookie.</p>

</body>
</html>
if you set a cookie, the
only time its available is
upon the next refresh of
the page. You can't set a
cookie, then immediately
read its value without
refreshing the page first.
Delete a Cookie
• To delete a cookie, use
the setcookie() function with an expiration
date in the past :
Delete a Cookie
• <!DOCTYPE html>
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>

<?php
echo "Cookie 'user' is deleted.";
?>

</body>
</html>

You might also like