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

Connect.php

<?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());
}

login.php

<?php
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";
}
}
?>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1></h1>
<form action="" method="post">
<label>User Name :</label>
<input type="text" name="username" /><br />
<label>Password</label>
<input type="password" name="password" /><br />
<input type="submit" value="Login" name="submit"/>
</form>
<?php

?>
</body>
</html>

LEAVE A REPLY

Please enter your comment!
Please enter your name here