Techumber
Home Blog Work

Simple Login Signup With PHP

Published on August 29, 2012

In this post i will explain, how to create a simple login signup system for your php web application. You can download the complete source code Example form below link. Simple Login Signup system using PHP

Demo

Download

Create these files. -config.php -index.php(login) -signup.php -logout.php

Frist we need to create database for saving registed used data. So lets create a User DataBase Table form below code. If you using localhost(xampp) first create a sample database from phpmyadmin. Later execute this code on that database.

Users Database Table

CREATE TABLE IF NOT EXISTS`user"` (
`id` int(4) NOT NULL auto_increment,
`username` varchar(65) NOT NULL default '',
`pass` varchar(65) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1

config.php

<?php
//You need to change below values match to your settings.
$server = 'localhost'; //mysql server name
$user = 'mysqluser'; //mysql user name
$pass = 'mysqlpass'; //mysql password
$db = 'dbname'; //database name
$tbl_name="User"; //table name
//Connecting to database
$con = mysql_connect($server, $user, $pass) or die ("Could not connect to server ... \n" . mysql_error ());
//Selecting db
mysql_select_db($db) or die ("Could not connect to database ... \n" . mysql_error ());
?>

login.php

<?php
session_start();
//includeing config file for db settings.
include('config.php');
//Check if it is post request came by clicing Sign in button
if($_POST['signin_btn']=="Signin")
{
//this will hold error message.
$err=null;
// username and password sent from form and filtering for any harmful code
$user=mysql_real_escape_string(stripslashes($_POST['user']));
$pass=mysql_real_escape_string(stripslashes($_POST['pass']));
//Check wether username,password filed are not empty.
if(empty($user)){
$err='<strong>Error!</strong>please enter username';
}
elseif(empty($pass)){
$err="<strong>Error!</strong>please enter password";
}
else{
//check wether user with pass exits in db if yes we will create session.
$sql="SELECT * FROM $tbl_name WHERE username='$user' and pass='$pass'";
$count=mysql_num_rows(mysql_query($sql));
if($count==1){
$_SESSION['user']=$user;
header("location:".$_SERVER['PHP_SELF']."?msg=1");
}
else{
$err='<strong>Error!</strong>Please enter Valid username/password';
}
}

}
<?php
if($_SESSION['user']){
echo '<div class="alert alert-success">
<strong>HI '.$_SESSION['user'].'!</strong> You are already Logged in.
<a href="logout.php">log out here</a>
</div>';
}
else if($_GET['msg']){
echo '<div class="alert alert-success">
<strong>Well done!</strong> You successfully Logged in.
<a href="logout.php">log out here</a>
</div>';
}
else{
if($err){
echo '<div class="alert alert-error">
'.$err.'
</div>';
}
else if($_GET['status']){
echo '<div class="alert alert-success">
<strong>Well done!</strong> You successfully SigUP.
Please SignIn below
</div>';
}
?>

<form class="form-horizontal mini-layout" action="" method="post">
<legend>Sign in</legend>
<div class="control-group">
<label class="control-label" for="inputEmail">User Name</label>
<div class="controls">
<input type="text" id="inputEmail" name="user" placeholder="User Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" id="inputPassword" name="pass" placeholder="Password">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
<button type="submit" class="btn btn-info" name="signin_btn" value="Signin">Sign in</button>
<a class="btn btn-link" href="signup.php">Don't have account?</a>
</div>
</div>
</form>
<?php } ?>

-In above code first we are including config.php in which we have db-connection settings. -Filtering user inputs. -validating user inputs. -Querying database for user entered username and password, If match found session will be created and page will display success message.

signup.php

<?php
session_start();
//includeing config file for db settings.
include('config.php');
//Check if it is post request came by clicing Sign up button
if($_POST['signup_btn']=="SignUp"){
//for holding error messsage.
$err=null;
//feltering the inputs
$user=mysql_real_escape_string(stripslashes($_POST['user']));
$pass=mysql_real_escape_string(stripslashes($_POST['pass']));
$pass2=mysql_real_escape_string(stripslashes($_POST['pass2']));
//checking if the username and password fields are empty
if(empty($user)){
$err="<strong>Error!</strong>please enter username";
}
else if(empty($pass)||empty($pass2)){
$err="<strong>Error!</strong>please enter password";
}
//cheking if the both password not same
else if($pass!=$pass2){
$err="<strong>Error!</strong>please enter same password";
}
else{
//inserting record in to table
$result=mysql_query("INSERT INTO ".$tbl_name."(username,pass) VALUES('".$user."','".$pass."');") or die("error in syntex");
//createing session variable
$_SESSION['user']=$user;
header("location:index.php?status=1");
}
}
if($_SESSION['user']){
echo '<div class="alert alert-success">
<strong>HI '.$_SESSION['user'].'!</strong> You are already Logged in.
<a href="logout.php">log out here</a>
</div>';
}
else if($_GET['msg']){
echo '<div class="alert alert-success">
<strong>Well done!</strong> You successfully Logged in.
<a href="logout.php">log out here</a>
</div>';
}
else{
if($err){
echo '<div class="alert alert-error">
'.$err.'
</div>';
}
else if($_GET['status']){
echo '<div class="alert alert-success">
<strong>Well done!</strong> You successfully SigUP.
Please SignIn below
</div>';
}
}
?>

<form class="form-horizontal mini-layout" action="" method="post">
<legend>Sign in</legend>
<div class="control-group">
<label class="control-label" for="inputEmail">User Name</label>
<div class="controls">
<input type="text" id="inputEmail" name="user" placeholder="User Name">
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
<input type="password" id="inputPassword" name="pass" placeholder="Password">
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
<button type="submit" class="btn btn-info" name="signin_btn" value="Signin">Sign in</button>
<a class="btn btn-link" href="signup.php">Don't have account?</a>
</div>
</div>
</form>
<?php } ?>

In this if user inputs are valid we will add new record to the User Table and redirect to the index page with status code 1.This will display success message.

logout.php

<?php
session_start();
//remove session value.
session_destroy();
header("location:index.php");
?>

In the logout.php code we will destroy the session which we have created earlier and redirect to the home page. Hope! you enjoyed this post. Please write your comments below.