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

Home Sql Mysql-example PHP SQL Login

Related Tutorials


Share on Google+Share on Google+


This Application illustrates how to create a login page where only authorized user can login and process further.



This Application illustrates how to create a login page where only authorized user can login and process further.

In this application, we have created a userlogin.php page which has a form with two input fields and one submit button. In this form, we validate  whether both input field are filled or not. If the input field is blank then an alert message will be displayed. If user fills the correct credentials then loginaction.php page will is called which works as the action page where we check both field from database table whether user name and password is available in the database table or not. If user name and password have been validated then user is sent to the welcome.php page and if the user is not authorized then to the error.php page with error message and a link for come back on userlogin.php page.

Source Code of userlogin.php 

  <title>User Login Page</title>
  <script language="JavaScript">
  function validateForm(theForm){
  if(theForm.userid.value ==""){
  alert("Enter the name");
  return false;
  alert("enter the password");
  return false;
  return true;

  <table border="1" cellspacing="0" cellpadding="0">
  <h2>Login Form</h2>
  <form method="POST" action="loginaction.php" onsubmit="return validateForm(this);">
  <font size="2"> <strong> Enter username and Password: </strong></font>
  <td><b> UserName</b></td>
  <td><input type="text" size="20" name="userid"></td><br>
  <td><b> Password </b></td>
  <td><input type="password" size="20" name="password"></td>
  <td><input type="submit" name="submit" value="submit"></td>

Source Code of loginaction.php 

  if( !isset($_SESSION) ) { session_start(); }

  $link=mysql_connect($host_db,$user_db,$password_db) or die ("couldnot connect: ".mysql_error());
  mysql_select_db($database_db, $link)  or exit('Error Selecting database: '.mysql_error()); ;

  $errormessage = "";

  $sql="SELECT * FROM users  where username='$userid' and password='$password'";
  $result = mysql_query($sql, $link)  or exit('$sql failed: '.mysql_error()); 
  $num_rows = mysql_num_rows($result);
  header("Location: error.php");
  } else {
  header("Location: welcome.php");


<h2>Login Success...</h2>


<h2>Login Failed...</h2>
<a href="userlogin.php">login again</a>

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 17, 2009

Related Tutorials

Discuss: PHP SQL Login   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 15, 2011
SQL Injection

hahaha. Your script is vulnerable to SQL injection... I hope you don't use this script on your site.
February 17, 2012

please i will need a process of setting up an sql database please make it as simple as possible even if i can get a copy and paste version :) Thanks
September 7, 2012
Doesnt work

Its not working for me. Some how when i open the php file using firefox it tends to download the file. Please help.
September 26, 2012
conectivity in sql server

i want 2 create admin page in php and connectivity in sql server so plz guide me.....
September 22, 2013
html php sql oracol