First of all i'll assume that :
1 - you are using PHP 5 with mysql 4 or greater .
2 - you have some knowledge about HTML , PHP , Mysql .
So lets start
we have 3 pages :
1 - login.php // to allow users enter their Username & pass word .
2 - check.php // to check the username and password and create session .
3 - hello.php // the page requires Permissions to get viewed .
The Data base side has a table with 3 columns :
1 - Username column(Field) .
2 - Password column(Field) .
3 - UserID column(Field) .
This is a form for login.php which contains username & pass word text area :
Code: Select all
<form method="POST" action="check.php">
<p align="left">
Username :<input name="T1" size="20" style="float: left"></p>
<p> </p>
<p align="left">Password :</p>
<p><input name="T2" size="20" style="float: left"></p>
<p align="left"> </p>
<p align="left"><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
<p> </p>
</form>
now lets assume the user enters his username and password . . . .
so we have to get them in the check.php page and compare them with the saved username and pass word in the Database .
we will use
$_post['T1'];
to get the entered username
and
$_post['T2'];
to get the entered password .
so in the check.php put this code :
Code: Select all
$Entered_username = $_POST['T1'];
$Entered_password = $_POST['T2'];
Then we will Create a Mysql Connection to get the stored usernames and password using this code :
Code: Select all
// Establish a connection with Mysql DataBase
mysql_connect ('localhost','root',''); // DataBase Username = root & Database password = Nothing
mysql_select_db('users'); // Our database name = users
// Establish a connection with Mysql DataBase
now we have the entered username and password and we r connected to the Mysql database .
to get the usernames and password we use this SQL query :
Code: Select all
$query = "Select * from users";
$result = mysql_query($query);
Then we will go throw usernames and passwords we got from the database (stored in $result)
& compare them with our entered username & pass word using this code :
Code: Select all
while ($row = mysql_fetch_array($result))
{
extrat($row);
if ($Entered_password == $Username && $Entered_password == $Password )
{
session_start();
echo "<a href=hello.php>Loged in Successfully Press Here to go to the next page</a>";
}
}
if (!session_start())
{
echo "Username or Password is incorrect press back button and try again later";
}
now it's all done just we need to check if the user is loged in or no when he want to enter hello.php page we will do it using this code :
Code: Select all
if (!session_start())
{
echo "Username or Password is incorrect press back button and try again later";
}
// Get The username & passwords
// Check
if (session_start())
{
echo "Welcome :D";
}
else
{
echo "Please login using this page : login.php";
}
Hope i helped u enough
sorry for my bad language <<< English is not my mother tongue

.
Best Wishes
Tarek Nagi
http://www.tareknagi.com
Conect - Dev Team