Posted by: tonyteaching | July 10, 2009

Login Page

Often we need Login Page for pages restricted for specific users.

To make it, we need to create 4 files and 1 table (storing usernames and passwords) in a database

1. a Page that has content of FORM for login. e.g. “loginform.html” (this form will have action=”file for taking action of the input data”..e.g. “checklogin.php”)

2. a file for receiving input data from login form (then opening and connecting with database & table which store username and password, alternatively the list of username and password can be put as static data on this file directly as PHP script), –> then verifying the username and password, –>  . e.g. “checklogin.php”, –> then heading to next file (the restricted file)

3. The login_success file, e.g. “login_success.php” (for checking the session is registered on not, so the system will reject if user accesses manually typing the file location…also here is the restricted can be HTML sintax)

4. logout.php (for destroying the session)

————————————————– The Scripts:

1. “loginform.html”


<form name=”login” action=”checklogin.php” method=”post”>

Username: <input name=”myusername” id=”myusername” size=”15″ maxlength=”15″ type=”text” /><br >
Password: <input name=”mypassword” id=”mypassword” size=”15″ maxlength=”15″ type=”password” />        <br>
<input value=”LOGIN” type=”submit” name=”submit”/>




2. checklogin.php

$host=”localhost”; // Host name
$username=”…”; // Mysql username
$password=”….”; // Mysql password
$db_name=”…”; // Database name
$tbl_name=”….”; // Table name

// Connect to server and select databse.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

// Define $myusername and $mypassword

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql=”SELECT * FROM $tbl_name WHERE username=’$myusername’ and password=’$mypassword'”;

// Mysql_num_row is counting table row
// If result matched $myusername and $mypassword, table row must be 1 row

// Register $myusername, $mypassword and redirect to file “login_success.php”
else {
echo “Wrong Username or Password”;



3. login_success.php


<title>Restricted Page</title>
<H1> This is the restricted Page </H1>
<a href=”logout.php”>Logout</a>


4. logout.php


// Logged out, return home.
Header(“Location: index.html”);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: