Easy Tutorial
❮ Func Ftp Connect Func Date Timezone Location Get ❯

PHP Cookie


Cookies are often used to identify users.


What is a Cookie?

Cookies are often used to identify users. 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 to Create a Cookie?

The setcookie() function is used to set a cookie.

Note: The setcookie() function must appear before the <html> tag.

Syntax

setcookie(name, value, expire, path, domain);

Example 1

In the following example, we will create a cookie named "user" and assign the value "tutorialpro" to it. We also set the cookie to expire after one hour:

<?php
setcookie("user", "tutorialpro", time()+3600);
?>

<html>
.....

Note: When sending a cookie, the cookie's value is automatically URL-encoded, and automatically decoded when received. (To prevent URL encoding, use setrawcookie() instead.)

Example 2

You can also set the expiration time of a cookie in a different way. This might be simpler than using seconds.

<?php
$expire=time()+60*60*24*30;
setcookie("user", "tutorialpro", $expire);
?>

<html>
.....

In the above example, the expiration time is set to one month (60 seconds * 60 minutes * 24 hours * 30 days).


How to Retrieve a Cookie's Value?

The $_COOKIE variable is used to retrieve a cookie's value.

In the following example, we retrieve the value of the cookie named "user" and display it on the page:

<?php
// Output the cookie value
echo $_COOKIE["user"];

// View all cookies
print_r($_COOKIE);
?>

In the following example, we use the isset() function to confirm whether a cookie has been set:

<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>

<?php
if (isset($_COOKIE["user"]))
    echo "Welcome " . $_COOKIE["user"] . "!<br>";
else
    echo "Guest!<br>";
?>

</body>
</html>

How to Delete a Cookie?

When deleting a cookie, you should make the expiration date a past time.

Deletion example:

<?php
// Set the cookie expiration time to one hour ago
setcookie("user", "", time()-3600);
?>

What if the Browser Does Not Support Cookies?

If your application needs to interact with browsers that do not support cookies, you have to use other methods to pass information between pages in your application. One way is to pass data via forms (about forms and user input, we have covered in previous chapters of this tutorial).

The following form submits user input to "welcome.php" when the user clicks the "Submit" button:

<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name">
Age: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html>

Retrieve the values in the "welcome.php" file as shown below:

<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>

Welcome <?php echo $_POST["name"]; ?>.<br>
You are <?php echo $_POST["age"]; ?> years old.

</body>
</html>
❮ Func Ftp Connect Func Date Timezone Location Get ❯