PHP simple Login & Remember me script using Cookies
PHP simple Login & Remember me script using Cookies Zip:
In this tutorials, we will learn how to create PHP Login with 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
Solution:
PHP Cookies are used to store small amount of information on browser than 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.
Here is the code of page1.php:
PHP User Form
<?php
/**
* Script Name: PHP Form Login Remember Functionality with Cookies
* Source: www.TutorialsClass.com
**/
?>
<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>
2) This page will save username/password data in cookies if remember box is checked. If remember box is not checked, cookies will be cleared/deleted. You can check login page again to verify if cookies are saved or not.
Here is the code of page2.php.
Save username/password in cookies if remember me is selected
<?php
/**
* Website: www.TutorialsClass.com
**/
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 user checked remember me, user details will be automatically loaded in form fields next time. That means that now your browser remember your username & password.
If user unchecked remember me, cookies will be deleted and browser does not remember user details. That means that form fields will be empty and user has to enter his username/password again.
How to use run above code script:
You can copy code from first box into a page1.php and code from second box into page2.php. Now you can test this code by entering user detail along with remember me checkbox or without remember me checkbox.
This is a very simple and easy PHP script to test PHP Cookies with Login form. You can simply download script and run on your computer as well.
PHP simple Login & Remember me script using Cookies Zip:
Tutorials |
---|
No Content Found. |
Exercises & Assignments |
---|
No Content Found. |
Interview Questions & Answers |
---|
No Content Found. |