Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Sql Mysql-example PHP SQL Login Script

Related Tutorials


Share on Google+Share on Google+

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


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

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

Source Code of checklogin.php 

  mysql_connect("$host", "$username", "$password")or die("cannot connect");
  mysql_select_db("$db_name")or die("cannot select DB");


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


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

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

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

  } 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 


Login Successful


Download Source Code




If you enjoyed this post then why not add us on Google+? Add us to your Circles

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

Related Tutorials

Discuss: PHP SQL Login Script   View All Comments

Post your Comment

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

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
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