PHP MYSQL based Login System - Justclickin
You Are Here: Home » Web Development » PHP MYSQL based Login System

PHP MYSQL based Login System

This is the second part of my earlier post PHP MYSQL based registration form, if you have missed it please read it here

You can use the same database for this one.php+msq+login

Simple PHP MYSQL based Login System

We basically need 9 files for this

  • index.php
  • db.php
  • form.php
  • login.php
  • loggedin.php
  • page.css
  • checklogin.php
  • profilepic.php
  • logout.php

Let’s see the codes for each of the files

Index.php

This page contains only an include tag to include the form.php

<div id="content"><?php include('form.php');?></div>

db.php

This file contains codes for database connection

<?php
$mysql_hostname = "localhost";//mysql host
$mysql_user = "root";//mysql user
$mysql_password = "";//mysql password
$mysql_database = "jci";//mysql db
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong with connection");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong! can't select db!");
?>

form.php

<div>
<div align="center">
<form name="login" method="post" action="login.php">
<table width="200" border="0">
<tr>
<td height="23"><table width="200" height="52" border="0">
<tr>
<td width="62">username</td>
<td width="122">
<input name="user" type="text" id="user" value="<?php if(isset($_POST['user'])) echo htmlspecialchars($_POST['user']);?>">
</td>
</tr>
<tr>
<td>password</td>
<td>
<input name="pass" type="password" id="pass" >
</td>
</tr>
</table></td>
</tr>
<tr>
<td><label>
<a href="http://demo.justclickin.com/registration/index.php">Register Here</a>
<div align="center">
<input name="login" type="submit" id="login" value="login">
</div>
</label></td>
</tr>
</table>
</form>
</div>
</div>

login.php

<div id="content"><?php
include('db.php');
if(!isset($_POST['login']))//checking if user has entered this page directly
{
include('form.php');
}
else{
if(isset($_POST['user'])&&$_POST['user']==""||!isset($_POST['user']))
{
$error[] = "Username Field can't be left blank";
$usererror = "1";
}
if(!isset($usererror))
{
$user = mysql_real_escape_string($_POST['user']);
$sql = "SELECT * FROM member WHERE user = '$user'";
if(mysql_num_rows(mysql_query($sql))=="0")//1 means there is one entry same so we print error
{
$error[] = "Can't find a user with this username";

}

}
if(isset($_POST['pass'])&&$_POST['pass']==""||!isset($_POST['pass']))
{
$error[] = "password Field can't be left blank";
}
if(isset($error)){
if(is_array($error)){echo "<div class=\"error\"><span>please check the errors and refill the form<span><br/>";
foreach ($error as $ers) {
echo "<span>".$ers."</span><br/>";
}
echo "</div>";
include('form.php');
}
}
if(!isset($error)){
$suser=mysql_real_escape_string($_POST['user']);
$spass=md5($_POST['pass']);//for secure passwords
$find = "SELECT * FROM member WHERE user = '$suser' AND password = '$spass'";
if(mysql_num_rows(mysql_query($find))=="1"or die(mysql_error())){
session_start();
$_SESSION['username'] = $suser;
header("Location: loggedin.php");
}
else{
echo "<div class=\"warning\"><span>Some Error occured durring processing your data</div>";
}
}
}//close else
?></div>

loggedin.php

<?php include('checklogin.php');?>
<html>
<head>
<style>
#content{
margin:0 auto 0 auto;
width:700px;
margin-top:100px;
background-color: #FFFFFF;
-moz-box-shadow: 10px 10px 10px #BBB;
-webkit-box-shadow: 10px 10px 10px #BBB;
box-shadow: 10px 10px 10px #BBB;
border: 1px solid #CCC;
padding:5px;
}
</style>
</head>
<body>
<div id="content">
<div align="center">hellow, <?php echo htmlspecialchars($_SESSION['username']) ?> <img src="http://www.gravatar.com/avatar/<?php include('profilepic.php'); ?>?s=32" width="32" height="32" /><a href="logout.php"> Logout</a> </div>
</div>
</body>
</html>

Page.css

This contains simple CSS code for error divs

.error{
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);
background-color: #FF5B5B;
width:500px;
padding:8px;
padding-left:30px;
margin:0 auto 0 auto;
border:thin solid #B70000;
background-image: url(red.png);
background-repeat: no-repeat;
background-position: 10px center;
}
.error span{
margin-left:20px;
color:#FFFFFF;}
.error li{

margin-left:20px;

color:#FFFFFF;

list-style:none;}

.success{

text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);

background-color: #339900;

width:500px;

padding:8px;

padding-left:30px;

margin:0 auto 0 auto;

border:thin solid #336600;

background-image: url(green.png);

background-repeat: no-repeat;

background-position: 10px center;}

.success span{

margin-left:20px;

color:#FFFFFF;}

.warning{

text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25);

background-color: #FF9900;

width:500px;

padding:8px;

padding-left:30px;

margin:0 auto 0 auto;

border:thin solid #FF6600;

background-image: url(yellow.png);

background-repeat: no-repeat;

background-position: 10px center;}

.warning span{

margin-left:20px;

color:#FFFFFF;}

checklogin.php

Now we have to find whether user has logged in or not

<?php
session_start();
if(!isset($_SESSION['username'])){
header("Location: index.php");
}
?>

profilepic.php

This will insert a small image of user from Gravatar

<?php
include('db.php');
$suser = mysql_real_escape_string($_SESSION['username']);
$sql = "SELECT * FROM member WHERE user = '$suser'";
$result = mysql_fetch_array(mysql_query($sql)) or die(mysql_error());
echo md5( strtolower( trim($result['email'] ) ) );
?>

Logout.php

<?php
session_start();
unset($_SESSION['username']);
header("Location: index.php");//redirecting to index page
?>

DOWNLOAD FILES 

So now you can create a simple Registration as well as a Login Page for your website with ease.  Let us know if you have any query by posting in your comments.

About The Author

Web Designer/Application Developer

Hi Friends, I am Sujay from Irinjalakuda (Thrissur Dst, Kerala). I am doing my AME (Aircraft Maintenance Engineering) from SHA-SHIB aviation Academy Cochin. I love creating websites and web applications. I also love travelling and learning new languages. Thats all about me.

Number of Entries : 3

Comments (5)

  • Raghav Narang

    Cool One Bro. Just A Little Request. Please Tell About Some Encryption Ways, So That One Can Make All The Accounts More Secure :)

    Reply
  • Sujay Sreedhar

    md5 is the bestway of all.md5 also can be cracked (well not exactly what they’ll do is check for same hash) so what we should do is md5 the password with a key . So that will be hard to find example do code like this
    $newpass=$_POST['pass'].”your key”;
    $secpass =md5($newpass);
    Use the same code in login and registration system

    Reply
  • Petter

    I have a problem at the beginning of login… When i fill the form with correct information from DB it isn´t work on next step (loggedin.php)… Please help me.

    Reply
  • Ivan

    Thanks for this tutorial. I don’t know too much about the php so I ended up using phpSecurePages because it allows granular access. Why reinvent the wheel? I got it at phpsecurepages.com

    Reply
  • Grim

    Sir great tutorial but i cant find the first part of the tutorial… can u please upload it again.. thx

    Reply

Leave a Comment

Copyright © 2012–2013 Justclickin All Rights Reserved || Sitemap  DMCA.com

Genuine Online Jobs To Earn Money From HomeCheck It Out
Scroll to top