PHP Ajax Login System

Status
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.

nathanadhitya

Thread Starter
Joined
Jan 23, 2013
Messages
7
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
usercake.com sorry if my link was annoying here's my codes ive used and edited
login.php
PHP:
<?php 
require_once("includes/config.inc.php");
securePage($_SERVER['PHP_SELF']);

//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
if(!empty($_POST))
{
	$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
		if(!usernameExists($username))
		{
			$errors[] = lang("ACCOUNT_USER_OR_PASS_INVALID");
		}
		else
		{
			$userdetails = fetchUserDetails($username);
			//See if the user's account is activated
			if($userdetails["active"]==0)
			{
				$errors[] = lang("ACCOUNT_INACTIVE");
			}
			else
			{
				//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");
				}
				else
				{
					//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
					$loggedInUser->updateLastSignIn();
					$_SESSION["User"] = $loggedInUser;
					
					//Redirect to user account page
					header("Location: account");
					die();
				}
			}
		}
	}
}

require_once("includes/header.php");
if((isset($_POST['ajax']) || isset($_GET['ajax'])) && count($errors) > 0) die( implode('<br>', $errors) );
echo "
<body>
";
include("includes/edit-file/header.php");

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

echo resultBlock($errors,$successes);

echo "
<div id='regbox'>
<form name='login' action='".$_SERVER['PHP_SELF']."' method='post'>
<p>
<label>Username:</label>
<input type='text' name='username' />
</p>
<p>
<label>Password:</label>
<input type='password' name='password' />
</p>
<p>
<label>&nbsp;</label>
<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(http://adikzforum07.tk/styles/default/xenforo/gradients/form-button-white-25px.png) 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;' />
</p>
</form>
</div>
</div>
";
include("includes/edit-file/footer.html");
echo"
</body>
</html>";

?>
Login PopUP
HTML:
<div id="loginoverlay" onclick="loginsys();" style="display:none;"></div>
<div id="login" style="display:none;">
  <img src="http://illiweb.com/fa/empty.gif" class="closebutton" width="20px" height="20px" onclick="loginsys();"/>
  <h1>Log In</h1>
  <div id="login_response" align="center"></div>
  <form action="/login" method="post">
    <ul>
      <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>
    </ul>
    <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="">
  </form>
</div>
Login JS
Code:
function loginsys() {
   var x = document.getElementById('login');   
   if (x.style.display == 'none') {
      jQuery(x).add('#loginoverlay').fadeIn('fast');
      var r = x.getElementsByTagName('form')[0].redirect;
      r.value = window.location.href;
   } 
   else {
      jQuery(x).add('#loginoverlay').fadeOut('slow');
   }
}
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
 
Status
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

Top