Secure Login page using PHP

In my last post I explained how to code a registration page in PHP. Now if you have created your registration.php page successfully then its time to move ahead and learn how to create login page in PHP.  Using the same old database ‘SCHOOL’ and table ‘CLASS’ we will code a login.php script. Remember Internet is full of injectors and attackers so you need to make sure that only authentic people can login into your system. I have written this login script which will  prevent your website from SQL injection and XSS attacks. Always consider some serious security risks while developing your project.

Function of login.php script:

This login script will take username and password entered by user through login.html page.Then it will match that username and password combination in database and when it finds that combination exists in table it will allow user to login else it will show appropriate error message.

SQL query for ‘CLASS’ table:

  CREATE TABLE IF NOT EXISTS <code>class</code> (
 <code>id</code> int(2) NOT NULL AUTO_INCREMENT,
 <code>name</code> varchar(20) NOT NULL,
 <code>password</code> varchar(50) NOT NULL,
 PRIMARY KEY (<code>id</code>)

Code for login.html:

<head><title> Login Page</title>
<form action="login.php" method="post">
<input type="text" name="fname" />
<input type="password" name="passwd" />
<input type="submit" value=" Submit "/>

Now here goes the code for login.php script:


$connection = mysql_connect("localhost","root","12345") or
die("Opps some thing went wrong");
$db_select=mysql_select_db("school",$connection) or
die("Opps some thing went wrong");

$name = strip_tags(substr($_POST['fname'],0, 100));
$safename = mysql_escape_string($name);
$password = strip_tags(substr($_POST['passwd'],0,100));
$safepassword = mysql_escape_string($password);
$passwordenc = md5($safepassword);

/* Using strip_tags() and substr() to cut off intruders input, you can
not trust users input blindly. mysql_escape_string() is used
 to prevent sql injection */

$sql="SELECT id FROM class WHERE name='$safename' and

echo $result;

// If result matched $name and $password, table row must be 1 row
header("location: admin.html");
//echo $error="Your Login Name or Password is invalid";


If you have any query or any doubt then please feel free to ask.

About sanjeev

Dreamer, Blogger and Thinker. I love to help people in solving their problems. You can also join me HERE

Related posts:

7 Responses so far.

  1. Wonderful blog you have here but I was curious if you knew of any forums that cover the same topics discussed in this article? I’d really love to be a part of group where I can get advice from other experienced individuals that share the same interest. If you have any suggestions, please let me know. Bless you!

  2. Hi getting this error ( ! ) Parse error: syntax error, unexpected ‘<' in C:\wamp\www\login\login.php on line 29

    It refers to this code:

    header(“location: admin.html”);

    Any help would be great


Leave a Reply

Your email address will not be published. Required fields are marked *