PHP Cookies
Cookies are often used to identify users. A cookie is a small file that the server leaves on the user's computer. This computer will send cookies whenever the same computer requests a page through a browser. With PHP, you can create and retrieve the value of a cookie.
How do I create cookies?
The Setcookie () function is used to set cookies. Note: the Setcookie () function must precede the
- <?php
- Setcookie ("User", "Alexporter", Time () +3600);
- ?>
-
- .....
Note: When a cookie is sent, the value of the cookie is automatically encoded and automatically decoded when it is retrieved. (To prevent URL encoding, use Setrawcookie () instead.) )
How do I get back the value of a Cookie?
PHP's $_cookie variable is used to retrieve the COOKIE's value. In the following example, we retrieve the value of the cookie named "User" and display it on the page:
- <?php
- Printacookie
- echo$_cookie["User"];
- Awaytoviewallcookies
- Print_r ($_cookie);
- ?>
In the following example, we use the Isset () function to confirm that a cookie has been set:
-
- <body>
- <?php
- if (Isset ($_cookie["user"))
- echo "Welcome". $_cookie["user". "! <br> ";
- Else
- echo "Welcomeguest!<br>";
- ?>
- </body>
-
How do I delete cookies?
When you delete a cookie, you should change the expiration date to a past point in time. Deleted instances:
- <?php
- Settheexpirationdatetoonehourago
- Setcookie ("User", "", Time ()-3600);
- ?>
How JavaScript creates, acquires cookies