PHP SQL Login Script

In this example, we have created a login application where we will use php script for validation.

PHP SQL Login Script

PHP SQL Login Script

     

This Application illustrates how to create a php validation script for login application.

In this example, we have created a login application where we will use php script for validation. If the user does not fill the user name or password then the error message is displayed in the next page. If user passes unauthentic credentials then error message  "Incorrect User Name OR Password Found" is displayed to the user. Success message is displayed if user fills correct information. All script is in the checklogin.php where we create a connection between database table and application to check whether user is valid or not.

Source Code of login.php

<html>
<head><title>Login Form</title></head>
<body>
  <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  <tr>
  <form name="form1" method="post" action="checklogin.php">
  <td>
  <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
  <tr>
  <td colspan="3"><strong>Please Login Here</strong></td>
  </tr>
  <tr>
  <td width="78">Username</td>
  <td width="6">:</td>
  <td width="294"><input name="username" type="text" id="username"></td>
  </tr>
  <tr>
  <td>Password</td>
  <td>:</td>
  <td><input name="password" type="password" id="password"></td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td><input type="submit" name="Submit" value="Login"></td>
  </tr>
  </table>
  </td>
  </form>
  </tr>
  </table>
</body>
</html>

Source Code of checklogin.php 

<?php
  $host="localhost";
  $username="root";
  $password="root";
  $db_name="test";
  
  mysql_connect("$host", "$username", "$password")or die("cannot connect");
  mysql_select_db("$db_name")or die("cannot select DB");

  $username=$_POST['username'];
  $password=$_POST['password'];

  $username = stripslashes($username);
  $password = stripslashes($password);
  $username = mysql_real_escape_string($username);
  $password = mysql_real_escape_string($password);

  $flag="OK";  
  $msg="";  

  if(strlen($username) < 1){
  $msg=$msg."Please enter the user name<br>";
  $flag="NOTOK"; 
  }

  if(strlen($password) < 1 ){
  $msg=$msg."Please enter the password<br>";
  $flag="NOTOK";  
  }

  if($flag <>"OK"){
  echo "<left>$msg <br> <input type='button' value='Retry' onClick='history.go(-1)'></left>";
  }else{
  $sql="SELECT * FROM users WHERE username='$username' and password='$password'";
  $result=mysql_query($sql);

  $count=mysql_num_rows($result);
  
  if($count==1){
  session_register("username");
  session_register("password");
  header("location:loginsuccess.php");
  } else {
  echo "Incorrect User Name OR Password Found";
  echo "<left><br><input type='button' value='Retry' onClick='history.go(-1)'></left>";
  }
  }
?>

Source Code of loginsuccess.php 

<?
  session_start();
  if(!session_is_registered(username)){
  header("location:login.php");
  }
?>

<html>
<body>
Login Successful
</body>
</html>

 

Download Source Code

 

Output: