PHP Cookies

What’s a Cookie?

Often, a cookie is used to figure out who a user is. A cookie is a small file that is put on the user’s computer by the server. When the same computer’s browser requests a page, it will also send the cookie. With PHP, you can both make cookies and get their values back.
Use PHP to make cookies.

With the setcookie() function, you can make a cookie.

Syntax

 

setcookie(name, value, expiration, path, domain, secure, httponly);

 

Only Peter’s name is needed. All the other parts can be left out.

Here are the specifics of each argument.

Name: This sets the name of the cookie, which is saved in an HTTP COOKIE VARS environment variable. This variable is used when cookies are being used.

Value: This is the actual data you want to store. It sets the value of the named variable.

Expiry: This is a date and time in seconds from midnight GMT on January 1, 2050. After this time, you won’t be able to get to cookie. If this parameter is not set, the cookie will automatically end when the Web browser is closed.

Path: This tells the cookie what directories it can be used in. With just one forward slash, the cookie can be used in all directories.

Domain: This can be used to specify the domain name in very large domains. It must have at least two periods for it to be valid. Cookies can only be used by the host and domain that made them.

Security: If this is set to 1, it means that the cookie should only be sent using HTTPS. If it is set to 0, it means that the cookie can also be sent using regular HTTP.

 

Create or Get a cookie with PHP

In the next example, a cookie called “user” with the value “Ram” is made. After 30 days (86400 * 30), the cookie will no longer work. The “/” means that the cookie can be used on the whole website (otherwise, select the directory you prefer).

Then, we use the global variable $_COOKIE to get the value of the “user” cookie. We also check to see if the cookie is set with the isset() function:

Example

 

<?php

$cookiename = “name”;
$name = “ram”;
$cookieage = “age”;
$age = “25”;

setcookie($cookiename, $name, time()+2500, “/”,””, 0);
setcookie($cookieage, $age, time()+2500, “/”, “”, 0);
?>
<html>

<head>
<title>Setting A Cookies with PHP</title>
</head>

<body>
<?php echo “Set Cookies”?>

<?php
if(!isset($_COOKIE[$cookiename])) {
echo “Cookie named ‘” . $name . “‘ is not set!”;
} else {
echo “Cookie ‘” . $name . “‘ is set!<br>”;
echo “Name is: ” . $_COOKIE[$cookiename];
}

if(!isset($_COOKIE[$cookieage])) {
echo “Cookie age ‘” . $age . “‘ is not set!”;
} else {
echo “Cookie ‘” . $age . “‘ is set!<br>”;
echo “Age is: ” . $_COOKIE[$cookieage];
}
?>

</body>
</html>

 

 

Note: The setcookie() function must come BEFORE the html> tag.

Note: When the cookie is sent, its value is automatically URLencoded, and when it is received, it is automatically decoded. If you don’t want this to happen, use setrawcookie() instead.

Modify a Cookie Value

To change a cookie, just use the setcookie() function to set it again:

Example

 

<?php

$cookiename = “name”;
$name = “ram”;
$cookieage = “age”;
$age = “25”;

setcookie($cookiename, $name, time()+2500, “/”,””, 0);
setcookie($cookieage, $age, time()+2500, “/”, “”, 0);
?>
<html>

<head>
<title>Setting A Cookies with PHP</title>
</head>

<body>
<?php echo “Set Cookies”?>

<?php
if(!isset($_COOKIE[$cookiename])) {
echo “Cookie named ‘” . $name . “‘ is not set!”;
} else {
echo “Cookie ‘” . $name . “‘ is set!<br>”;
echo “Name is: ” . $_COOKIE[$cookiename];
}

if(!isset($_COOKIE[$cookieage])) {
echo “Cookie age ‘” . $age . “‘ is not set!”;
} else {
echo “Cookie ‘” . $age . “‘ is set!<br>”;
echo “Age is: ” . $_COOKIE[$cookieage];
}
?>

</body>
</html>

Delete a Cookie

Use the setcookie() function with an expiration date in the past to delete a cookie.

Officially, you should call setcookie() with only the name argument to delete a cookie. However, this does not always work well and should not be relied on.

Setting a cookie with a date that has already passed is the safest thing to do.

Example

<?php
// set the expiration date to one hour ago
setcookie(“name”, “”, time() – 3600);
?>
<html>
<body>

<?php
echo “Cookie ‘name’ is deleted.”;
?>

</body>
</html>

 

Check to see if cookies enabled

The next example shows how to make a small script that checks if cookies are turned on. First, use the setcookie() function to try to make a test cookie. Then, count the $_COOKIE array variable:

Example

<?php
setcookie(“cookiename”, “test”, time() + 3600, ‘/’);
?>
<html>
<body>

<?php
if(count($_COOKIE) > 0) {
echo “Cookies are enabled.”;
} else {
echo “Cookies are disabled.”;
}
?>

</body>
</html>

People also search

Leave a Comment

Scroll to Top