Your Ad Here

Monday, August 24, 2009

Simple login using php (part 2)

First i will make a login form.

<form method="POST" action="login.php">

Username: <input type="text" name="userid" >
Password: <input type="password" name="password" >
<input type="submit" value="login" name="login">

</form>

Now lets have a look at login.php

<?PHP
//isset function checks for exsistence of the variable if isset returns false then it redirects to login page

if (!isset($userid) || !isset($password)) {
header( "Location: http" address of your login page " );
}
//here it checks if forms fields are submitted empty or not
//if empty it redirects to login page
elseif (empty($userid) || empty($password)) {
header( Location:"address of login page" );
}
else{




//addslashes() function is used to escape double quotes ("), single quotes ('), backslash (\) or NULL.It does so by automatically inserting a backslash (\) character in front of the characters in a string.
//addslashes() function is especially useful to use on a string before inserting into a database
$user = addslashes($_POST['userid']);
$pass = $_POST['password'];


//Store the form value into php variables

$dbHost = "localhost";
$dbUser = "Database userid";
$dbPass = "Database password ";
//as assumed earlies we have a login database
$dbDatabase = "login";

//conneting to the database

$db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error while connecting to database.");

mysql_select_db("$dbDatabase", $db) or die ("Couldn't select the database.");

$result=mysql_query("select * from users where username='$userid' AND password='$password'", $db);


//checks if result is not empty
$row = mysql_num_rows($result);
if($row > 0)
{
while($row = mysql_fetch_array($result))

{


//A session is started and variable registered
session_start();
session_register('userid');
//the session variable userid is used through out the users session

echo 'Successful login !';

//redirect to a page
//let check.php be the page where you want to redirect
header( "Location: http:"check.php");

}

}
else {

echo 'There was an error logging in please try again.';
}

?>



Please check the next post where i will discuss about how to check if user is logged in.

3 comments until now.

Mobile App Developers + October 12, 2015 at 10:28 PM (#) :

Great article, Thanks for your great information, the content is quiet interesting. I will be waiting for your next post.

Buy Contact Lenses + July 20, 2017 at 1:55 AM (#) :

This is the precise weblog for anybody who needs to seek out out about this topic. You notice so much its almost arduous to argue with you. You positively put a brand new spin on a subject that's been written about for years. Nice stuff, simply nice!

GST Training + August 9, 2017 at 1:57 AM (#) :

very informative post for me as I am always looking for new content that can help me and my knowledge grow better.

Post a Comment