What is a cookie
Cookies are used to store the information of a web page in a remote browser so that when the same user comes back to that page, that information can be retrieved from the browser itself.
In this tutorial, we will discuss how to use Cookies in PHP. We have several examples in this tutorial which will help you to understand the concept and use of a cookie.
Creating cookies
A cookie is created with the setcookie() function.
Syntax
setcookie(name, value, expire, path, domain, secure, httponly);
Let’s see the parameters used:
Parameter | Description | Which type of data |
---|---|---|
name | Name of the cookie. | String |
value | Value of the cookie, stored in the client’s computer. | String |
expire | Unix timestamp, i.e. number of seconds since January 1st, 1970 (called Unix Epoch). | Integer |
path | Server path in which the cookie will be available. | String |
domain | To which domain the cookie is available. | String |
secure | If set true, the cookie is available over a secure connection only. | Boolean |
httponly | If set true, the cookie is available over HTTP protocol only. Scripting languages like JavaScript won’t be able to access the cookie. | Boolean |
Only the name parameter is required, other parameters are optional.
PHP Create or Retrieve Cookie
The following example creates a cookie named “user” with the value “Rajesh”. The cookie will expire after 30 days (86400 * 30). The “/” means that the cookie is available in the entire website (otherwise, select the directory you prefer).
We then retrieve the value of the cookie “user” (using the global variable $_COOKIE). We also use the isset() function to find out if the cookie is set:
<!DOCTYPE html> <?php $cookie_name = "user"; $cookie_value = "Rajesh"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day ?> <html> <body> <?php if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> <p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p> </body> </html>
Note:
- The setcookie() function must appear BEFORE the <html> tag.
- The value of the cookie is automatically URLencoded when sending the cookie, and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).
Modify or Edit a Cookie Value
To modify a cookie, just set (again) the cookie using the setcookie() function:
<!DOCTYPE html> <?php $cookie_name = "user"; $cookie_value = "Ramesh"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); ?> <html> <body> <?php if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?> <p><strong>Note:</strong> You might have to reload the page to see the new value of the cookie.</p> </body> </html>
Delete a Cookie
To delete a cookie, use the setcookie() function with an expiration date in the past:
<!DOCTYPE html> <?php // set the expiration date to one hour ago setcookie("user", "", time() - 3600); ?> <html> <body> <?php echo "Cookie 'user' is deleted."; ?> </body> </html>
PHP simple Login & Remember me script using Cookies
Here we are going to create PHP Login & Remember me functionality using Cookies.
Code Description:
- Create a simple PHP Form with username (text field), password (password field), remember (checkbox) & Login (submit) button.
- If user select ‘remember’ functionality, save username & password in cookies
- When Login Form loads, show recently saved username & password from the cookies
- If user do not select ‘remember’, cookies will be deleted
PHP Cookies are used to store a small amount of information on a browser that can be used later for different purposes. We will use PHP Cookies to remember user details. You can find HTML form code in page1.php & set/delete cookies on page2.php
1) We will create a form with username & password fields. User can fill details & click on login/submit button. After login/submission, form data will be passed/redirected to another page (page2.php). This form will show recent username/password automatically, if user has clicked remember me before.
<!DOCTYPE html> <?php /** * Script Name: PHP Form Login Remember Functionality with Cookies **/ ?> <html> <body> <form action="page2.php" method="post" style="border: 2px dotted blue; text-align:center; width: 400px;"> <p> Username: <input name="username" type="text" value="<?php if(isset($_COOKIE["username"])) { echo $_COOKIE["username"]; } ?>" class="input-field"> </p> <p>Password: <input name="password" type="password" value="<?php if(isset($_COOKIE["password"])) { echo $_COOKIE["password"]; } ?>" class="input-field"> </p> <p><input type="checkbox" name="remember" /> Remember me </p> <p><input type="submit" value="Login"></span></p> </form> </body> </html>
2) This page will save username/password data in cookies if the remember box is checked. If the remember box is not checked, cookies will be cleared/deleted. You can check the login page again to verify if cookies are saved or not.
<?php if(!empty($_POST["remember"])) { setcookie ("username",$_POST["username"],time()+ 3600); setcookie ("password",$_POST["password"],time()+ 3600); echo "Cookies Set Successfuly"; } else { setcookie("username",""); setcookie("password",""); echo "Cookies Not Set"; } ?> <p><a href="page1.php"> Go to Login Page </a> </p>
Result: If the user checked to remember me, user details will be automatically loaded in form fields next time. That means that now your browser remembers your username & password.
If the user unchecked remember me, cookies will be deleted and the browser does not remember user details. That means that form fields will be empty and the user has to enter his username/password again.