Building a Login Form using PHP/MySQL
This tutorial shows you how to build a simple login form and to check the login credentials entered against te database using PHP and MySQL.I had introduced some PHP lessons earlier.It might be worth to take a look at them before you go through this.Anyway,this isnt any harder.I have specially included this as an article because a login form is an essential component of most dynamic websites.
Again we are using MySQL database.Create a database(here i have created one called as “test”).Now create a table ‘login’ with two fields namely ‘username’ and ‘password’.Make ‘username’ the primary key as we do not want the same username to be used more than once.
CREATE TABLE `test`.`login1` (
`username` VARCHAR( 30 ) NOT NULL ,
`password` VARCHAR( 30 ) NOT NULL ,
PRIMARY KEY ( `username` )
)
Now all that is needed is a login form and some PHP code to read the values entered in the textboxes and to verify them against the values in the database. Here is the code of login.php:
<html>
<head>
<title>
<h2>Login Form</h2>
</title>
</head>
<body>
<?php
//display the form if it has not been submitted yet
if(!isset($_POST['submit'])){
?>
<form method="post" action="login.php">
Username:<input type="text" name="username"/></br>
Password:<input type="password" name="password"/></br>
<input type="submit" name="submit" value="Login"/>
</form>
<?php
//if the form has been submitted,check entered values against the database
}
else
{
$username=$_POST['username'];
$password=$_POST['password'];
//attempt the database connection
$mycon=mysql_connect("localhost","root","");
if(!$mycon)
{die('Could not connect'.mysql_error());}
mysql_select_db("test",$mycon);
$result=mysql_query("select * from login where username='$username' and password='$password'");
$num=mysql_numrows($result);
if($num==0)
{
echo "Incorrect Login Details";
}
else
{
echo "Login Sucessful";
}
mysql_close($mycon);
}
?>
</body>
</html>
The below fig illustrates how the form will look like.
Here when the form is submitted,the secnd half of the script comes into play.First,we save the entered values into two variables and then run the query “select * from login where username=’$username’ and password=’$password’”. Now the idea behind this is that if there is a row satisfying the above query,the number of rows returned will be more than zero.Also,we only need to check if the number of rows is more than zero since the username is a primay key and hence there can be only one row that can satisfy the above query.
The $num=mysql_numrows($result); code will assign the number of rows to the $num variable.All we have to check now is whether the variable is zero or not.If it is zero,then the login credentials submitted are incorrect else you have submitted the right username and password.
Finally dont forget to close the connection.Even though it will not generate an error in the above case,it is always a good practice to close the connection to keep the server running well.
The above form is a very simple login form.However,we will be using encrypted passwords in most cases.In that case,before running the query,encrypt the entered password using the same encryption technique used when the values were submitted to the database.To learn more about encryption in PHP,follow this link.
Related posts:









Leave your response!