Skip to main content

Login System Using PHP, Mysql ,Ajax And Twitter Bootstrap

Today we are going to create a Login System using PHP, Mysql('As Database'), Ajax(' For scriptin') And Twitter Bootstrap('For Designing'). Although we can create an simple log-in system but the major disadvantage of this type of log-in system is that your page is refreshed every time when you press submit or Log-in button this is such a waist of time and bandwidth. So the better way is to create an login for with Ajax so if your username and password is correct only then you page will be refreshed and you will be sent to the next success page So lets We Start....

DOWNLOAD


An twitter Bootstrap Is being Used for the Designing. So making an login form is easy using Bootstrap. Read Full Artical Here

Creating An Form 
<form class="form-horizontal" method="post" action="" id="login_form">
					  <div class="control-group">
						<label class="control-label" for="username">Username</label>
						<div class="controls">
						  <input type="text" id="username" placeholder="Username">
						</div>
					  </div>
					  <div class="control-group">
						<label class="control-label" for="password">Password</label>
						<div class="controls">
						  <input type="password" id="password" placeholder="Password">
						</div>
					  </div>
					  <div class="control-group">
						<div class="controls">
						  <label class="checkbox">
							<input type="checkbox"> Remember me
						  </label>
						  <input name="Submit" type="submit" id="submit" value="Login" class="btn btn-success"/>
						  <input type="reset" name="Reset" value="Reset" class="btn"/>
						</div>
					  </div>
					</form
Now We have created A Login Form Now we can write the Ajax Script That will Pick the Data From The Page And Then It will Pass That Data To our Ajax-login Page That Means To the Page where we are going to check The Login information Provided by the Person.


<script type="text/javascript">
$(document).ready(function()
{
	$("#login_form").submit(function()
	{
		//remove all the class add the messagebox classes and start fading
		$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
		//check the username exists or not from ajax
		$.post("ajax_login.php",{ user_name:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
        {
		  if(data=='yes') //if correct login detail
		  {
		  	$("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
              function()
			  { 
			  	 //redirect to secure page
				 document.location='logsuccess.html';
			  });
			  
			});
		  }
		  else 
		  {
		  	$("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Your login detail sucks...').addClass('messageboxerror').fadeTo(900,1);
			});		
          }
				
        });
 		return false; //not to post the  form physically
	});
	//now call the ajax also focus move from 
	$("#password").blur(function()
	{
		$("#login_form").trigger('submit');
	});
});
</script>>

So after That Script our Infromation Will be send to the ajax_login.php  . so we have to create an Ajax_login Page that will receive our values which are being sent by ajax and then after processing will send the result to our page.

ajax_login.php page



<?php session_start();
//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use

//Connect to database from here
$link = mysql_connect('localhost', 'root', ''); 
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
//select the database | Change the name of database from here
mysql_select_db('testing'); 

//get the posted values
$user_name=htmlspecialchars($_POST['user_name'],ENT_QUOTES);
$pass=md5($_POST['password']);

//now validating the username and password
$sql="SELECT user_name, password FROM tbl_user WHERE user_name='".$user_name."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);

//if username exists
if(mysql_num_rows($result)>0)
{
	//compare the password
	if(strcmp($row['password'],$pass)==0)
	{
		echo "yes";
		 //Regenerate session ID to prevent session fixation attacks
    session_regenerate_id();


		//now set the session from here if needed
		$_SESSION['u_name']=$user_name; 
		$member=mysql_fetch_assoc($result);
		$_SESSION['u_id']=$member['id'];
		$name_show=$member['first_name'].' '.$member['last_name'];
		$_SESSION['name']=$name_show;
			//Write session to disc
			session_write_close();

		}
	else
		echo "no"; 
}
else
	echo "no"; //Invalid Login
?>
So now if The user is authenticated then in a result there will be a success message shown on the page. And user will be redirected to a success page and Success message will be shown to the user. Hope you like this if any query then you can leave a comment...
Thank You!!!!

Comments

  1. Replies
    1. i didn't quite understand which file are you asking for???

      Delete
  2. Not work.....where demo????????????????????

    ReplyDelete
    Replies
    1. Really Sorry for that sir... We are working on demo..
      mean while i will check and let you know why it's not working.

      Delete

Post a Comment

your Comment is sent for moderation, Thankyou

Popular posts from this blog

Network Topologies (Tutorial-4)

Topology refers to the way in which the network of computers is connected. The choice of topology is dependent upon--- Type and number of equipment being used  Planned applications and rate of data transfers  Required response times  Cost Types of Network Topologies Physical Topology: Physical topology defines how devices are connected to the network through the actual cables that transmit data(physical structure of the network)  Logical Topology: Logical Topology (also referred to as Signal Topology) is a network computing term used to describe the arrangement of devices on a network and how they communicate with one another.

Cisco Hierarchical Model

Cisco Hierarchical Model Defined by Cisco to simplify the design, implementation, and maintenance of responsive, scalable, reliable, and cost-effective networks. The 3 layers are logical and not physical – there may be many devices in a single layer, or a single device may perform the functions of 2 layers, eg: core and distribution. The Cisco Hierarchical Model

Windows 7 transformation pack for xp

As the windows 7 launched after that there are lot of screenshot available on the internet, There developer designed the transformation packs for xp to make it better lo0king like windows 7. There are some transformation packs for your xp. Download Vienna 2.5 – Windows 7 Transformation Pack (Mirror) Downloa d HFN Windows 7 Transformation Pack Download Windows 7 Transformation pack – NSFormation (Mirror) Download Vienna Transformation Pack v1