Very first question in every one’s mind is “Why to use PHP Login Script ?”, The reason is that you might need a login authentication from some of your users, depending on your application to restrict their rights(what they can do and what they can’t).
If i have to save my data from non-authenticated people then all i need to do is to create a login page to save my data from non-serious users.
Let us move to the development phase. I have to create following files:
- login_form.php
- login.php
- success.php
- logout.php
I will follow some important steps to complete this tutorial:
- Take some Input from User(userName, Password)
- Hit submit button, to send this data to the server
- Match the user input with the Data Base entries
- At this step i have two possibilities
- If data matches then redirect user to success page
- if data does not matches then ask User to Re-Enter the information
First i need to create a table that contains three fields (admin_id,admin_usr_name,admin_password)
After creating a table, i have inserted two values into it using the following Query
I got this Table
Let us create a Form to take input from user as i have shown in the following image:
![]() |
login_form.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<html>
<body>
<table width="200" border="0" cellspacing="1" align="center">
<form id="form1" name="form1" method="post" action="login.php">
<tr>
<td colspan="2"><h2>Members Login</h2></td>
</tr>
<tr>
<td>UserName:</td>
<td>
<input type="text" name="username" id="username" />
</td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" id="password" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="btnSubmit" id="btnSubmit" value="Log In" />
</td>
</tr>
</form>
</table>
</body>
</html>
|
Now we have to create a login Script that will be executed on Server.
login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
//*********Server Information to establish a connection ******
$host = 'localhost'; // Server Host Name
$user = 'root'; // Server User Name
$password = 'vertrigo'; // Server Password
$db = 'dbsneaker'; // Your Database
$link = mysql_connect($host,$user,$password) or die('Error in Server information');
mysql_select_db($db,$link) or die('Can not Select Databasse');
//***************End Connection Establishment***************************************
//*******Form Information********
$userName = mysql_real_escape_string($_POST['username']); //User Name sent from Form
$password = mysql_real_escape_string($_POST['password']); // Password sent from Form
//*********retrieving data from Database**********
$query = "select * from tbladmin where admin_usr_name='$userName' and admin_pwd='$password'";
$res = mysql_query($query);
$rows = mysql_num_rows($res);
//**********if $userName and $password will match database, The above function will return 1 row
if($rows==1)
//***if the userName and password matches then register a session and redrect user to the Successfull.php
{
$_SESSION['userName'];
$_SESSION['password'];
header("location:success.php");
}
else
{
echo 'Data Does Not Match <br /> Re-Enter UserName and Password';
}
?>
|
Now once this is executed , I need to create a success message and to display this success message i have to create a success.php Script
success.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
session_start();
if($_SESSION['userName']!='')
{
header("location:login_form.php");
}
else
{
echo '<h2>Successfully Login <br /> Welcome '.$userName.'</h2>';
echo '<a href="logout.php"> Log Out</a>';
}
?>
|
NOw its important to destroy the registered session and to destroy this session, I have created a logout script.
logout.php
1 2 3 4 5 6 7 8 |
<?php
session_start();
//*****session_destroy() will destroy the session
session_destroy();
header("location:login_form.php");
?>
|
If you find any kind of error, then please comment and ask me to solve that so that i can make this script perfect. It will rejoice me and make me confident. Feel free to comment.
Random Posts










Pingback: Registration Form PHP Script :