PHP Login Registration Scripts

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:

PHP Simple Login and Registration Script

PHP Simple Login and Registration Script:


What is PHP Login and Registration Script?

This is a simple login and registration script based on PHP & MySQL. This script uses PHP session to store user details across multiple PHP pages to verify if the user is logged in or not. This script has minimal HTML/PHP/MySQL code which is easy to understand.


How to run Login-Registration Script:

You can simply download this script as a zip file, extract and run on your machine.

Here are the quick steps to run this script:

  • Download this script into a separate folder
  • Import database.sql file in a database ‘register_login_db’
  • Now run this folder’s index.php file. You will find ‘login’ & ‘registration’ link on this page.
  • Register some users and try to login with them.
  • You will be able to access page-1.php and page-2.php content only after login.
  • You can logout/login many times to test it.

Understanding code in each file

PHP Login and Registration script includes HTML Forms, PHP Session code & interacts with MySQL database to access users data. You will find that most of the code is well managed in a separate file. Here is the list of files along with their purpose.

  • database.sql: This file contains a query to create table structure for the login-register script. You can either import this file or directly execute given queries in PHP-Myadmin or available database management tool.
  • register.php: This file have an HTML registration form where user can enter his name, email and password. If registration data is valid, it will give success, else it will throw error.
  • login.php: This file contains HTML login form where user can enter email and password. The Same page keep PHP code to check if given email/password is matched or not. If login is successful, it will redirect user to page-1 content.
  • db-config.php: This file contains PHP code to create a connection with MySQL database and table. Login & Registration page includes this file to interact with MySQL data.
  • page-1.php & page-2.php: These files contains sample content which are only accessible after login. If you try to access these pages without login, it will redirect you to login screen.
  • logout.php: This file is used to destroy user data in PHP Session. This means that user will be logged out and redirected to login page.

How PHP Login and Registration code works

Mostly PHP scripts use PHP session or cookies to remember user details for login. This script also uses PHP session to track user login across multiple PHP pages to verify if the user is logged in or not.

Here is the basic idea behind PHP login and registration functionality.

  • Firsly, registration form allow users insert user details in database.
  • Then, user login using his registered email and password. Now if email and password found in database, user login considered successful. After successful login, we will save user email in PHP session variable.
  • Now on each content page, we will check if user email is available in session variable or not. If available, user can view content pages else he will be redirected to login page.
  • If a user clicks on logout link, PHP session gets destroyed and user needs to login again to view content pages.
  • This is how Registration/Login works to protect certain pages/features only for authorized/logged-in users.

Common Mistakes while running Login and Registration Script

  • You need to download and extracted all files preferably in separate folder.
  • Make sure you have created database properly with the same as mentioned in db-config.php
  • When running login-registration script, make sure folder name is right. Sometimes there is folder inside folders. Adjust your path or folder accordingly.
  • Understand the overall data flow using comments in each file.
  • If you found any issue with the script, please send your error screenshot at our facebook page. We will try to assist you if our schedule permits.

This PHP Login & Registration code is also available in Gitlab: https://gitlab.com/tutorialsclass/php-simple-login-registration-script

Download Simple PHP Login and Registration Script as Zip and try: