Home Sql Mysql-example PHP SQL Login Script
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

PHP SQL Login Script

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

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:

Advertisement

Liked it!  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.

Posted on: January 20, 2009

Ask Questions?    Discuss: PHP SQL Login Script   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Riz
February 20, 2013
Login

Incorrect password is working fine but for correct details I get the below message. Can you help? Deprecated: Function session_is_registered() is deprecated in D:\Hosting\10525984\html\Login\Login2\loginsuccess.php on line 3 Login Successful
beetsma
April 17, 2012
doesnt work for me

so as the title said: it doesnt for me : If i try to login it says : cannot select DB. I changed the $db_name=""; to my own databse, but it wont work. any suggestions? thx for sharing the script
DMCA.com