In our previous tutorial we discuss on how to create a secure registration page using three different approaches. They are:
mysql: How to Create Secure Registration Page in PHP/MySQL Part I
mysqli: How to Create Secure Registration Page in PHP/MySQL Part II
PDO: How to Create Secure Registration Page in PHP/MySQL Part III
mysqli: How to Create Secure Registration Page in PHP/MySQL Part II
PDO: How to Create Secure Registration Page in PHP/MySQL Part III
This time we will create a secure login script based on our previous tutorial. So be sure to read it especially on how to create our database.
I will combine two approaches here begining with mysql extension.
mysql extension
Now let's create the login form.
login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form id="form1" name="form1" method="post" action="login.php"> <table width="510" border="0" align="center"> <tr> </tr> <tr> </tr> <tr> </tr> <tr> </tr> </table> </form> </body> </html>
login.php
<?php $username = $_POST['username']; $password = $_POST['password']; $query = "SELECT password, salt FROM member WHERE username = '$username';"; { } if($hash != $userData['password']) // Incorrect password. So, redirect to login_form again. { }else{ // Redirect to home page after successful login. } ?>
Note, that we are still using the mysql_real_escape_string to secure our login page. Plus using password hashing with salt.
mysqli extension
login.html
Modify the above code from:
<form id="form1" name="form1" method="post" action="login.php">
to
<form id="form1" name="form1" method="post" action="login2a.php">
login2a.php
Procedural style
Procedural style
<?php $username = $_POST['username']; $password = $_POST['password']; $query = "SELECT password, salt FROM member WHERE username = '$username';"; { } if($hash != $userData['password']) // Incorrect password. So, redirect to login_form again. { }else{ // Redirect to home page after successful login. } ?>
As you can see, we just changed some few line based on our previous script called login.php. This is because we are using procedural style of mysqli extension.
Code equivalent:
mysql_connect() = mysqli_connect()
mysql_query() = mysqli_query()
mysql_connect() = mysqli_connect()
mysql_query() = mysqli_query()
login2b.php
Object Oriented style
Again, change the action properties under form tag in login.html script from login2a.php to login2b.php
Object Oriented style
Again, change the action properties under form tag in login.html script from login2a.php to login2b.php
<?php $username = $_POST['username']; $password = $_POST['password']; $mysqli = new mysqli('localhost', 'root', '', 'login'); $username = $mysqli->real_escape_string($username); $query = "SELECT password, salt FROM member WHERE username = '$username';"; $result = $mysqli->query($query); if($result->num_rows == 0) // User not found. So, redirect to login_form again. { } if($hash != $userData['password']) // Incorrect password. So, redirect to login_form again. { }else{ // Redirect to home page after successful login. } ?>
On procedural style we use the following code and its equivalent in object oriented style:
mysqli_connect() = new mysqli()
mysqli_query() = $mysqli->query()
mysqli_query() = $mysqli->query()
In our next tutorial, we will create a new article to differentiate PDO from the above code. PDO is a bit different so we will not cover it here.
Write Comment...
EmoticonEmoticon