WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Cookies


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 leaves on a user's computer. Whenever the same computer requests a page through a browser, that computer sends a cookie. With PHP, you can create and retrieve cookie values.


How do I create a cookie?

setcookie () function is used to set a cookie.

Note: The setcookie () function must be located <html> Before the label.

Syntax

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

Instance 1

In the following example, we will create a cookie named "user" and assign it a value of "welookups". We also require that this cookie expire after one hour:

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

<html>
.....

Note: When a cookie is sent, the value of the cookie is automatically URL-encoded and automatically decoded when retrieved. (To prevent URL encoding, use setrawcookie () instead.)

Example 2

You can also set cookie expiration time in another way. This may be simpler than using the seconds representation.

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

<html>
.....

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


How do I retrieve the value of a cookie?

PHP's $ _COOKIE variable is used to retrieve the value of the cookie.

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

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

//view all cookie
print_r($_COOKIE);
?>

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

<html>
<head>
<meta charset="utf-8">
<title>welookups Tutorial(welookups.com)</title>
</head>
<body>

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

</body>
</html>


How do I delete cookies?

When deleting cookies, you should change the expiration date to a point in the past.

Deleted instance:

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


What if the browser does not support cookies?

If your application needs to deal 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 through a form (we've covered forms and user input in previous sections of this tutorial).

The following form submitted user input to "welcome.php" when the user clicked the "Submit" button:

<html>
<head>
<meta charset="utf-8">
<title>welookups Tutorial(welookups.com)</title>
</head>
<body>

<form action="welcome.php" method="post">
first 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>welookups Tutorial(welookups.com)</title>
</head>
<body>

welcome <?php echo $_POST["name"]; ?>.<br>
you <?php echo $_POST["age"]; ?> Years old.

</body>
</html>