Jump to content

Always true statement.


NArc0t1c

Recommended Posts

Hello there,

 

I have an ajax login script, My problem is it is always returning the same variable even if the response is incorrect.

I have it posting to a php script that makes use of a php class in another script.

 

Here is my ajax login, it is two parts, Ajax.js and the actual login form, it is in a php class, i just removed it.

Ajax.php, I think this is fine.

function DoCallback(data)
{
// branch for native XMLHttpRequest object
if (window.XMLHttpRequest) {
	req = new XMLHttpRequest();
	req.onreadystatechange = processReqChange;
	req.open('POST', url, true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send(data);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
	req = new ActiveXObject('Microsoft.XMLHTTP')
	if (req) {
		req.onreadystatechange = processReqChange;
		req.open('POST', url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(data);
	}
}
}

function processReqChange() {
// only if req shows 'loaded'
if (req.readyState == 4) {
	// only if 'OK'
	if (req.status == 200) {
		eval(what);
	} else {
		alert('There was a problem retrieving the XML data:\n' +
			req.responseText);
	}
}
}

 

And the login form with its javascript, ignore the php $Display variable.

<script>
		var url = "Login_auth.php";
		var what = "LoginStatus(req.responseText)";
		function CheckLogin() {
			document.getElementById("status").className = "login_form";
			var username = document.getElementById("username").value;
			var password = document.getElementById("password").value;
			document.getElementById("status").innerHTML = "Checking..";
			if (!username){ 
			document.getElementById("status").innerHTML = "Username was left empty.";
			document.getElementById("status").className = "login_form_fail"; } 
			else if (!password){ 
			document.getElementById("status").innerHTML = "No password was given.";
			document.getElementById("status").className = "login_form_fail"; }
			else { 
			DoCallback("username="+username+"&password="+password);
			document.getElementById("status").className = "login_form"; } }

		function LoginStatus(Status){
			document.getElementById("password").value = "";
			if(Status == "0") document.getElementById("status").innerHTML = "Invalid Username.";
			else if(Status == "1") document.getElementById("status").innerHTML = "Invalid Password."; 				
			else  document.getElementById("status").innerHTML = "Correct.";  }
	</script>

	<div class="' . $Display .'">
	      <div id="status" class="login_form"></div>
	<pre>Username: <input id="username" type="text"><br>Password  <input id="password" type="password"><br><br><input type="button" value="Check Login" onClick="CheckLogin()">
	</pre>
	      </div>';

 

I have the script Ajax.js linked in the header script.

 

I'm also giving the php script Login_auth.php,

<?php

include("Classes/Initialize.Class.php");

if (isset($_POST)){
$UserName = $Misc->CleanString($_POST['username']);
$UserPassword = $Misc->CleanString($_POST['password']);
}
elseif (isset($_GET)){
$UserName = $Misc->CleanString($_GET['username']);
$UserPassword = $Misc->CleanString($_GET['password']);
}
else {
echo 'Hacking Attempt..';
        exit;
}
$Login->Authenticate($UserName, $UserPassword);
?>

 

Thanks

Ferdi

Link to comment
Share on other sites

  • 4 weeks later...

Ferdi, 

 

I placed a couple of alert statements in your code....

 

In :  processReqChange()

 

 

  evalOut                      = eval(what);

 

  alert('eval(what) : ' + evalOut)  // Displays second as:  eval(what) : undefined

 

 

First Line in In: LoginStatus(Status)

 

  alert('Status: ' + Status);  // Displays first as Status:

 

Hope this helps.

 

Scot L. Diddle, Richmond VA

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.