PHP Ajax Login System

This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.


Thread Starter
Jan 23, 2013
Hi, i'm trying to build an ajax login system on my own site here's my fully code that needed i'm currently using this app sorry if my link was annoying here's my codes ive used and edited

//Prevent the user visiting the logged in page if he/she is already logged in
if(isUserLoggedIn()) { header("Location: account"); if(isset($_POST['ajax']) || isset($_GET['ajax'])) die('1'); die(); }

//Forms posted
	$errors = array();
	$username = sanitize(trim($_POST["username"]));
	$password = trim($_POST["password"]);
	//Perform some validation
	//Feel free to edit / change as required
	if($username == "")
		$errors[] = lang("ACCOUNT_SPECIFY_USERNAME");
	if($password == "")
		$errors[] = lang("ACCOUNT_SPECIFY_PASSWORD");

	if(count($errors) == 0)
		//A security note here, never tell the user which credential was incorrect
			$errors[] = lang("ACCOUNT_USER_OR_PASS_INVALID");
			$userdetails = fetchUserDetails($username);
			//See if the user's account is activated
				$errors[] = lang("ACCOUNT_INACTIVE");
				//Hash the password and use the salt from the database to compare the password.
				$entered_pass = generateHash($password,$userdetails["password"]);
				if($entered_pass != $userdetails["password"])
					//Again, we know the password is at fault here, but lets not give away the combination incase of someone bruteforcing
					$errors[] = lang("ACCOUNT_USER_OR_PASS_INVALID");
					//Passwords match! we're good to go'
					//Construct a new logged in user object
					//Transfer some db data to the session object
					$loggedInUser = new loggedInUser();
					$loggedInUser->email = $userdetails["email"];
					$loggedInUser->user_id = $userdetails["id"];
					$loggedInUser->hash_pw = $userdetails["password"];
					$loggedInUser->title = $userdetails["title"];
					$loggedInUser->displayname = $userdetails["display_name"];
					$loggedInUser->username = $userdetails["user_name"];
					//Update last sign in
					$_SESSION["User"] = $loggedInUser;
					//Redirect to user account page
					header("Location: account");

if((isset($_POST['ajax']) || isset($_GET['ajax'])) && count($errors) > 0) die( implode('<br>', $errors) );
echo "

echo "
<div id='content'>";

echo resultBlock($errors,$successes);

echo "
<div id='regbox'>
<form name='login' action='".$_SERVER['PHP_SELF']."' method='post'>
<input type='text' name='username' />
<input type='password' name='password' />
<input type='submit' value='Login' class='submit' style='min-width: 85px; font-style: normal;
font-size: 12px;
font-family: Calibri, 'Trebuchet MS', Verdana, Geneva, Arial, Helvetica, sans-serif;
color: black;
background: url( 50% 0% repeat-x #DCDCEB;
padding: 0px 6px;
border: 1px solid;
border-image: initial;
border-top-left-radius: 7px;
border-top-right-radius: 7px;
border-bottom-right-radius: 7px;
border-bottom-left-radius: 7px;
text-align: center;
box-shadow: #C8C8D2 0px 1px 4px 0px;
-webkit-box-shadow: #C8C8D2 0px 1px 4px 0px;
text-shadow: transparent 0px 0px 0px, white 0px -1px 2px;
outline: none;
line-height: 23px;
display: inline-block;
cursor: pointer;
box-sizing: border-box;
height: 23px;' />

Login PopUP
<div id="loginoverlay" onclick="loginsys();" style="display:none;"></div>
<div id="login" style="display:none;">
  <img src="" class="closebutton" width="20px" height="20px" onclick="loginsys();"/>
  <h1>Log In</h1>
  <div id="login_response" align="center"></div>
  <form action="/login" method="post">
      <li><label for="username">Username</label></li>
      <li><input type="text" name="username" size="25" /></li>
      <li><label for="password">Password</label></li>
      <li><input type="password" name="password" size="25" /></li>
    <div style="clear:left"></div>
    <!-- <input type="submit" class="login-submit" name="login" value="Login" /> -->
    <button class="login-submit" name="login">Login</button>
    <input name="redirect" type="hidden" value="">
Login JS
function loginsys() {
   var x = document.getElementById('login');   
   if ( == 'none') {
      var r = x.getElementsByTagName('form')[0].redirect;
      r.value = window.location.href;
   else {
Ive Included Jquery Lastest In The Head
What i need is to make a ajax thats help validate the login
if no errors it will redirect to the same page it submitted then if there are some errors the errors will be forwarded to login_response
if anyone could help me please...
i need some help to build this
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online