2 Replies - 3297 Views - Last Post: 03 August 2010 - 09:05 PM

#1 DeathStory  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 13-November 09

AJAX and PHP Login Script

Posted 03 August 2010 - 11:23 AM

Hello I am currently making an iPhone Web App and I am trying to use as much AJAX and Javascript as possible instead of PHP (which I usually use instead of javascript as often as possible). In my script i call my javascript function checklog() which checks in the localStorage object for the username and password. If those two items have a non-null value then it uses ajax to send the username and password to my checklog.php file which then checks the username and password against a list of valid usernames and passwords (will eventually be a SQL Database) and then echo's either "yes" or "no". Then my checklog() javascript function is going to take the result of the PHP script and use jquery to load a different page depending on the response, but right now I just have it alerting either success or failure. That is where my problem is. When I'm trying to compare the response to either "yes" or "no" in an if statement they always return false even if i know the php script returned yes.

here is the javascript library :
var loginsuccess;

//I use this to set the username and password in the localStorage
function setLogin() {
localStorage.setItem('username', 'admin');
localStorage.setItem('password', 'root');
}

function checkLogin() {
var username = localStorage.getItem('username');
var password = localStorage.getItem('password');


if (username == null || password == null){
alert("Username and Password are NULL");
}

else {
var checklogurl = "/assets/includes/checklog.php?username=" + username + "&&password=" + password;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState == 4 && xmlhttp.status==200)
    {
//The next statement is because my web host adds extra to each page and I am removing that
//from the php response
    var success = xmlhttp.responseText.split("<");
    loginsuccess = success[0];
    if(loginsuccess == "yes"){
    alert("Success!");
      }
    if(loginsuccess == "no"){
    alert("Failure!");
     }
    }
  }
xmlhttp.open("GET", checklogurl, true);
xmlhttp.send();
 }
}
	



The index.php page where i have called these functions just loads and doesnt produce any errors or alert anything.

Here is the php script :

<?php

$checku = 'admin';
$checkp = 'root';

$username = $_GET['username'];
$password = $_GET['password'];

if($checku == $username && $checkp == $password){
echo "yes";
}

else{
echo "no";
}

?>


The problem is that I know that the PHP script is returning yes because I can alert(loginsuccess) and the output is yes. So I dont understand why the if statements are returning false and not executing? Can someone please help explain this to me and help me come up with a fix? I have tried every possible thing I can think of to make this work.

Is This A Good Question/Topic? 0
  • +

Replies To: AJAX and PHP Login Script

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,143
  • Joined: 07-September 06

Re: AJAX and PHP Login Script

Posted 03 August 2010 - 02:46 PM

Sorry, I don't have a LAMP currently accessable so I'll be asking a little bit extra out of you.

Could you post the output of this code:
var loginsuccess;

//I use this to set the username and password in the localStorage
function setLogin() {
localStorage.setItem('username', 'admin');
localStorage.setItem('password', 'root');
}

function checkLogin() {
var username = localStorage.getItem('username');
var password = localStorage.getItem('password');


if (username == null || password == null){
alert("Username and Password are NULL");
}

else {
var checklogurl = "/assets/includes/checklog.php?username=" + username + "&&password=" + password;
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState == 4 && xmlhttp.status==200)
    {
//The next statement is because my web host adds extra to each page and I am removing that
//from the php response
    var success = xmlhttp.responseText.split("<");
  // ! ADDED START
    alert(xmlhttp.responseText);
  // ! END ADDED
    loginsuccess = success[0];
    if(loginsuccess == "yes"){
    alert("Success!");
      }
    if(loginsuccess == "no"){
    alert("Failure!");
     }
    }
  }
xmlhttp.open("GET", checklogurl, true);
xmlhttp.send();
 }
}


I am expecting that you are having something odd such as your server adding a pre and post php page code to the output and as a result splitting the text at a "<" (opening caret) is causing it to break right before something like "<!DOCTYPE..." and return the blank string that is split("<")[0].

Sorry if that is a bit scatter brained, but I hope it will shed some additional light on the problem. If that isn't the case it will at least narrow down the possible issues.
Was This Post Helpful? 0
  • +
  • -

#3 DeathStory  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 13-November 09

Re: AJAX and PHP Login Script

Posted 03 August 2010 - 09:05 PM

Ahh I will do that tomorrow. It's late and I got off my computer awhile ago. I actually solved the problem earlier by changing the output of the php script to a number. I don't get why that helped though since Javascript isn't type casted and I wasn't using the strict assignment operator. I had already done that before and I got the output of yes and then I just got some text that 000webhost adds to the end of the web page (it annoys me because it always ruins me when I put the code through a validator). Even though I fixed the problem I still want to know why it wasn't working. I will paste the output of your alert in the morning.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1