A Simple Login Page In PHP
By bawinner
Coding a Login Page is not an uphill task as you may think.In this artcile,i'll try to keep it simple as you may want.
Pre-requisites for this is a PHP Enabled Server & a Database like MySQL.Let us assume that the user has signed up & the user data is stored in a table called "members".
So lets move on to create 3 pages which we would call as:Login.html,process.php & welcome.php.
Now,Let me explain the finer details of these pages.
(1)Login.html - Here we capture the user information from a form created in HTML.Let us consider the form has 3 Fields:Username Input(name it "userinput"),Password(name it "password") & a Submit button.
When the form is submitted it should lead to the next page,i.e.,the processing page.Let us call this page process.php
2)Process.php -
This page will capture the data submitted by the user from the Login.html page.
The code in PHP is as follows:
include "databasedetails.php"
/*This is a seperate php file created,which includes all your database details like Username,Password,etc...This is for easy entry of database details & for security reasons.*/
$username=$_POST["username"];//Capture the username entered by the user
$password=$_POST["password"];//Capture the password entered by the user
// Connect to server and select database.
mysql_connect("$hostname", "$username", "$password")or die("cannot connect server ");
/* $hostname,$username,$password,$databasename & $tablename are the respective details of your database in databasedetials.php file */
mysql_select_db("$databasename")or die("cannot select DB");
$query="SELECT * FROM $tablename WHERE username='$username' AND password='$password'";
/* The above query asks the table to return a Row where the username & password is the same as entered by the user */
$result=mysql_query($query);//Execute the above query
$count=mysql_num_rows($result);
/* Now lets count,how many rows are returned by the table matching our query.It should ideally be 1 provided that each username is unique.So if its 1,the member will be redirected to the Welcome Page else lets show an Error!Come,Lets Code it down */
if($count==1)
{
//Lets send the user to the Welcome Page!
REDIRECT THE USER TO THE WELCOME PAGE HERE!
}
else
{
echo"Sorry,The Username & Password did'nt match.Please Try Again!";
}
Dont forget to enclose your PHP Code in Tags.
Ah,i think we've reached the finishing point & i've finished my coffee too;-).Now the welcome page can be created to suit your needs.That's all.We've done it!
This's just a simple login page,for user verification.Security reasons demands us to write more advanced versions of a login page using session variables.Hope you understood atleast the basic logic behind a login page which was ultimately my aim.So,now,its time for you to practice it,Go Ahead!
Doubts or Suggestions are welcome through Comments!For more such tutorials and articles you can consider visiting my website www.anoopv.com.
Comments
No comments yet.