Share on Google+Share on Google+

PHP SQL Login Script

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

PHP SQL Login Script

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

In this example we create a login application where we put all php script for validation. In this application whether user is unauthorized then a error message will be display to inform that user is invalidate but if an authorized user login then a success message will be display. All script is on the checklogin.php where we create a connection between database table and application to check, 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($username1){
    $msg=$msg."Please enter the user name<br>";
    $flag="NOTOK"
  }

  if(strlen($password){
    $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:

 

Advertisements

Posted on: December 10, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.