Thursday, December 5, 2013

Easy PHP User Login System (Database User Validation)

Hiiiiii, people who are new-new php developers came across a situation when they have to develop a user login system. It seems to be a  very hard to develop login system by validating user details from database for the new developers but actually this task is very. You can send the username and password values to the next page or same page by just html form POST method or using AJAX. But this tutorial is  taking into cosideration that a developer is not having or completely unaware of knowledge of how to use AJAX for data sending , So, we will be just using core php to implement a login system.

I this tutorial, user is validated with the values exist i database, session variables like user id & username are set so that you can use that variables on anyother pages, form variables are sent to next page.

Demo :

PHP Login System

Code Begins :

File Structure : 
Main Directory --
--> check.php
--> index.php
--> home.php


check.php :

<?php
$link = mysql_connect('localhost', 'root', '');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db('test', $link);
if (!$db_selected) {
    die ('Can\'t use test table : ' . mysql_error());
}

$username=$_POST['username'];
$password=$_POST['password'];

$query="select * from user where username='".$username."'";
$result=mysql_query($query);
$records=mysql_fetch_array($result);

if($records)
{
 if($password==$records['password'])
 {
  session_start();
  $_SESSION['username']=$records['username'];
  $_SESSION['userid']=$records['id'];
  header('Location: home.php');
 }
 else
 {
  $msg="Password Incorrect";
  header('Location: index.php?msg='.$msg);
 }
}
else
{
 $msg="Username Incorrect";
 header('Location: index.php?msg='.$msg);
}
?>

index.php :

<!DOCTYPE HTML>
<html>
<head>
<title>Login System</title>
</head>
<body>

<h1>Login Form</h1>
<?php

if(isset($_GET['msg']))
{
 $msg=$_GET['msg'];
 echo '<p><font color="red">';
 echo $msg;
 echo '</font></p>';
}
?>
<form action="check.php" method=POST>
  <label for="username">Username</label>
  <input type="text" name="username" id="username" placeholder="username"><br>
  <label for="password">Password</label>
  <input type="password" name="password" id="password"  placeholder="password"><br><br>
  <input type="submit" value="Submit">
</form>
</body>
</html>



home.php :

<?php
session_start();
$username=$_SESSION['username'];
$userid=$_SESSION['userid'];

echo "You Are Logged IN -<b>".$username."</b> and your ID is -<b>".$userid."</b>";
?>
To run this tutorial , you need a database and a table containing users records. In this tutorial I am using a database namely - "test" with a table name - "user". This "user" table is having three columns as follows : id, username and password .


Hope you like this post……..Please Comment…………!!!!!!!!!


Tags: , ,

0 Responses to “Easy PHP User Login System (Database User Validation)”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks