This code will login a user based on the user is active or not, if the user is active, he is able to access otherwise he will be shown with an error message.

First, we are connecting to the database, and taking the input values from user, if the user name, password matches and the account is active we will allow him to the user’s area. Other wise we will display an error message.

We are creating two pages.

  1. connect.php
  2. login.php


$connection = mysql_connect('localhost', 'root', '');
if (!$connection){
die("Database Connection Failed" . mysql_error());
$select_db = mysql_select_db('test');
if (!$select_db){
die("Database Selection Failed" . mysql_error());


require_once 'connect.php';
if (isset($_POST['submit'])){
$username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT * FROM `confirm` WHERE username='$username' and password='$password' and active=1";
$result = mysql_query($sql) or die(mysql_error());
$count = mysql_num_rows($result);
if ($count == 1){
echo "You are logged in";
}else {
echo "Login Failed";
<form action="" method="post">
<label>User Name :</label>
<input type="text" name="username" /><br />
<input type="password" name="password" /><br />
<input type="submit" value="Login" name="submit"/>



Please enter your comment!
Please enter your name here