Login Using PHP and MySql

Sunday, 26 July 2015



Login page is most common for every websites and other applications. Simply we can say that this is the very common requirement.This is a kind of Authentication process. This is an authentication gate between the user request and other web pages. By using this process we can allow only authenticated users to our web sites. So in this article I am going to show the step by step process to implement this concept of login using PHP and MySql.

Following are the step by step process to implement the login using PHP and MySql, first try to observe each and every step then later you can implement your self.

  • Create a table name as user with two fields such as username and password.
  • Insert the data into the table.
  • Design the login form template with validation.
  • Write the code for the login process

Step 1 : (Create the user table)

This is a very simple step, hope you know that how to create a table in MySql, if not then just create a table name as user by using following SQL statements,


    CREATE TABLE user(
        username VARCHAR( 15 ) NOT NULL ,
        PASSWORD VARCHAR( 255 ) ,
        PRIMARY KEY ( username )
    )


Step 2 : (Insert data into table)

Use the following SQL query to insert the data into user table,


    INSERT INTO  `devinfo99`.`user` (
        `username` ,
        `password`
    )
    VALUES (
        'smith',  'smith@123'
    ), (
        'jhon',  'jhon@123'
    );


Step 3 : (Design login form template)

In this step we are going to design a template for login page using HTML and for form validation we will use JavaScript code.Create a file and save it as login_view.html and then copy the following code to this file.


<!DOCTYPE html>
<html>
	<head>
		<title> [ info ] | Our Research is Your Develop </title>
		<script>
			function validation(){
				var user	=	document.getElementById('user');
				var pswd	=	document.getElementById('pswd');
				if(user.value.length  ==  ''){
					alert("Please enter User Name");
					user.focus();
					return false;
				}else if(pswd.value.length  ==  ''){
					alert("Please enter Password");
					pswd.focus();
					return false;
				}else{
					return true;
				}
			}
		</script>
	</head>
	<body>
		<form method = "post" action = "login_logic.php" onsubmit = "return validationundefined)">
			<table>
				<tr>
					<td> User Name </td>
					<td> <input type = "text" name = "user" id = "user"> </td>
				<tr>
				<tr>
					<td> Password </td>
					<td> <input type = "password" name = "pswd" id = "pswd"> </td>
				<tr>
				<tr>
					<td></td>
					<td> <input type = "submit" name = "login" value = "Login"> </td>
				<tr>
			<table>
		</form>
	<body>
</html>


Step 3 : (Code for login process)

This is very important step, in this step we will write the complete logic for login process. So what you need to do is, just create the four .php files name as login_logic.php, welcome.php, invalid_user.php and logout.php. Copy the following codes to the respective files accordingly.

login_logic.php


<?php
     // Start the session 
 
     session_start();
 
     // Database Connection Parameters Details
 
     $host_name = "localhost";
     $user_name = "root";
     $password = "";
     $db_name = "devinfo99";
 
     // Get the user posted values from login form
 
     $user = $_POST['user'];
     $pswd = $_POST['pswd'];
 
     // Connect to the database
 
     mysql_connect($host_name, $user_name, $password);
     mysql_select_db($db_name);
 
     // Prepare SQL Statement 
 
     $sql_stmt = "SELECT * FROM user WHERE username =  '$user' AND PASSWORD =  '$pswd'";
 
     // Run the above query and check the availability of username and password
 
     $query = mysql_query($sql_stmt);
     $row = mysql_num_rows($query);
 
     // If username and password are available then login success other wise fail
 
     if($row == 1){
         $_SESSION['user_name'] = $user;
         header('Location: welcome.php');
     }else{
         header('Location: invalid_user.php');
     }
?>


welcome.php


<?php
	session_start();
	if(isset($_SESSION['user_name'])){
	    echo "Welcome to " . $_SESSION['user_name'];
            echo "<br />";
	    echo "<a href = 'logout.php'> Logout </a>";
	}else{
	    header('Location: login_logic.php');
	}
?>

invalid_user.php


<?php
    echo "Invalid username / password";
?>


logout.php


<?php
	session_start();
	session_destroy();
	header('Location: login_view.html');
?>


Note :
Following is explanation of each code of .php file 
  • login_logic.php : contains the code for Database connection, availability of username and password.
  • welcome.php : contains the code to show the welcome message to user after login successful.
  • invalid_user.php : If username and/or password is not available in Database then user will redirect to this page by saying message "Invalid User Name / Password".
  • logout.php : Contains the code to destroy the session and redirection to login_view.html.
Note :

You need to create the folder structure as follows,

LoginApp
      |
      |
      |------------> login_view.html
      |
      |------------> login_logic.php
      |
      |------------> welcome.php
      |
      |------------> invald_user.php
      |
      |------------> logout.php
      |

Download                                                                                  Demo

No comments:

Post a Comment

 
About Contact Career Advertise Job Support Online Training Acardemic Projects Internship
Copyright © 2016. [ info ].
Design by Herdiansyah Hamzah. & Distributed by Free Blogger Templates
Creative Commons License