Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP Cookies


May 11, 2021 PHP


Table of contents


PHP Cookies

Cookies are a mechanism for storing data on the remote browser side and using it to track and identify users, and PHP transparently supports HTTP 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 a server leaves on a user's computer. E ach time the same computer requests a page through a browser, the computer sends a cookie. With PHP, you can create and get back the value of the cookie.


How do I create cookies?

The setcookie() function is used to set cookies.

Note: The setcookie() function must be before the label.

Grammar

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

Instance 1

In the following example, we'll create a cookie called "user" and assign it "Alex Porter." We also specify that this cookie expires after one hour:

<?php

setcookie("user", "Alex Porter", time()+3600);

?>


<html>

<body>

···

</body>

</html>

Note: When a cookie is sent, the value of the cookie is automatically URL coded and automatically decoded when it is picked up. ( To prevent URL encoding, use setraw cookies() instead.)

Instance 2

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

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

<html>

<body>

···

</body>

</html>

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


How do I get the value of a cookie back?

PhP's $_COOKIE variable is used to get back the value of the cookie.

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

 <?php
 // Print a cookie
 echo $_COOKIE["user"];

 // A way to view all cookies
 print_r($_COOKIE);
 ?> 

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

 <html>
 <body>

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

 </body>
 </html> 


How do I delete cookies?

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

Deleted instances:

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


What if my browser doesn't support cookies?

If your application needs to deal with a browser that does not support cookies, you will have to use other methods to pass information between pages in your application. One way is to pass data through forms (as described earlier in this tutorial for forms and user input).

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

 <html>
 <body>

 <form action="welcome.php" method="post">
 姓名:<input type="text" name="name">
 年龄:<input type="text" name="age">
 <input type="submit">
 </form>

 </body>
 </html> 

Get back the value .php the "welcome" file, as follows:

 <html>
 <body>

 欢迎 <?php echo $_POST["name"]; ?>.<br><?php echo $_POST["age"]; ?> 岁了.

 </body>
 </html> 

Related tutorials

HTTP cookies are detailed