Your Ad Here

Monday, August 24, 2009

Simple login using php (part 1)

Hey today i am going to explain a simple login using php session.
In this first post i will be discussing about simply writing user infromation into a database.

Create a Mysql table which will contain all the login information of the user.
Coloumdata type
useridvarchar(12)
passwordvarchar(12)
emailvarchar(12)

step2: Create form for the users to register.
<form action="insert.php" method="post">
Username : <input type="text" name="userid" >
<br>
Password : <input type="password" name="password" >
<br>
E-mail: <input type="text" name="email" >
<br>
<input type="submit" value="submit" name="submit">
</form>
when user submit's this form it will go to insert.php

insert.php
this php code will insert value into the databse.
<?php


$conn = mysql_connect("localhost","Database username","database password");

//let us assume a database named login

$db = mysql_select_db("login");

// the values entered in the form are stored in variables
$userid = $_POST["userid"];
$password = $_POST["password"];
$email = $_POST["email"];


$result = mysql_query("insert into users(userid,password,email)"."values('$userid','$password','$email')");

echo "thanks for registering your account with us";

?>

Now the details of user is added into the database.
Please check out the next post to see the main part of login using session.

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.

Simple login using php (part 3)

Here i am going to discuss about the check.php.
This php page will check if the user is logged in or not . If not it will redirect the user to login page.

<?php

//session starts here
session_start();

//check if session is registered
if(session_is_registered('userid'))

{

//if registered display the content you would like to show
echo 'Hello you are logged in.';

}

else{

//if session variable is not registere send back user to login page
header( "Location: http://login.html" );
}

?>

Simple login using php (part 4)

Here i will explain about logging out of the session.

<?php
//session starts here
session_start();

//makes sure userid is registered
if(session_is_registered('userid')){

//if session is registered only then user can log out
session_unset();
}
session_destroy();
else{

//if user is not registered then the logout should not work and redirect user to login page
header( "Location: http://login.html" );
}
?>