4 Replies - 3031 Views - Last Post: 15 June 2011 - 08:45 AM

#1 GrueKun   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 21-November 10

Using AJAX to test MySQL login credentials: not working.

Posted 14 June 2011 - 07:30 PM

Hello everyone. First I'd like to say that I am pretty new to scripting languages such as javascript and PHP, but I'm much newer to things like AJAX and DOM.

I'm working on a personal project of mine (a type of bulletin board software) over the summer, and I wanted to write an installation script for it. At one point during the installation process, the script asks the user for their MySQL username/password and hostname. I thought it'd be kind of nice if I could use AJAX to test the credentials they enter without the user having to refresh the page.

I put together what, to me, looks like a working combination of AJAX and PHP.

The javascript function (that I know gets executed when the test button on the document is clicked) takes the username, password, and hostname and sends an XMLHttpRequest out using the POST method to make the database testing script (mysql_test.php) run a quick test to see if the credentials work or not. Depending on the text response, AJAX will write to the document and report whether the test was a success or failure.
function testMySqlCredentials() {
 var xmlhttp;
 var uname = document.forms["stepOneForm"]["mysqlUsername"].value;
 var passwd = document.forms["stepOneForm"]["mysqlPassword"].value;
 var host = document.forms["stepOneForm"]["mysqlHostname"].value;
 if (window.XMLHttpRequest) {
  //Modern browsers will support this.
  xmlhttp=new XMLHttpRequest();
 }
 else {
  //Ancient ones will not.
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 xmlhttp.onreadystatechange = function() {
  if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
   if (xmlhttp.responseText == "WORKS") {
    document.getElementById("mysql_test").style.color = "green";
	document.getElementById("mysql_test").innerHTML = "Credentials are good.";
   }
   else if (xmlhttp.responseText == "BAD USERNAME OR PASSWORD") {
    document.getElementById("mysql_test").style.color = "red";
	document.getElementById("mysql_test").innerHTML = "Username or password are bad.";
   }
   else if (xmlhttp.responseText == "BAD HOSTNAME") {
    document.getElementById("mysql_test").style.color = "red";
	document.getElementById("mysql_test").innerHTML = "Hostname provided did not work.";
   }
   else if (xmlhttp.responseText == "OTHER SERVER ERROR") {
    document.getElementById("mysql_test").style.color = "red";
	document.getElementById("mysql_test").innerHTML = "Other server error encountered. Try again later.";
   }
  }
 }
 xmlhttp.open("POST","mysql_test.php",true);
 xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
 xmlhttp.send("uname=" + uname + "&passwd=" + passwd + "&host=" + host);
}



Server side, the script is supposed to only see if it is able to log into the MySQL server. If it is successful, it reports that it "WORKS." If it is not successful, it analyzes the mysql error code and returns whether it was related to failed authentication or a bad hostname.

<?php
 // This script tests to make sure specified MySQL credentials are valid.
 
 // Remember to escape the strings.
 $username = addcslashes($_POST['uname']);
 $password = addcslashes($_POST['passwd']);
 $hostname = addcslashes($_POST['host']);
 
 $con = mysql_connect($hostname,$username,$password);
 if ($con == false) {
  if (stristr(mysql_error(), "ERROR 1045") != false) {
   echo "BAD USERNAME OR PASSWORD";
  }
  elseif (stristr(mysql_error(), "ERROR 1042") != false) {
   echo "BAD HOSTNAME";
  }
  else {
   echo "OTHER SERVER ERROR";
  }
 }
 else {
  echo "WORKS";
 }
 mysql_close($con);
?>


While both scripts look like they should work to me, I'm not sure if it's an error in javascript or an error in the PHP end of things.

Any ideas? Criticism is appreciated.

Thank you. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Using AJAX to test MySQL login credentials: not working.

#2 GrueKun   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 21-November 10

Re: Using AJAX to test MySQL login credentials: not working.

Posted 14 June 2011 - 11:45 PM

Well, I fixed it. At line 15, I had to capitalize the "S" in readyState, otherwise that entire block was skipped! I wasn't aware that javascript was that snippy about case sensitivity, but you live and learn. :/

I also changed the PHP script so that if there is a mysql error, it compares against stuff that is actually, ya know, returned by mysql_error(). I also changed it so that it escaped the POST data with addslashes() instead of addcslashes().

I'm just happy I had Chrome to help do the debugging. I might not have noticed the typo otherwise.

This post has been edited by GrueKun: 14 June 2011 - 11:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4214
  • View blog
  • Posts: 13,317
  • Joined: 08-June 10

Re: Using AJAX to test MySQL login credentials: not working.

Posted 15 June 2011 - 04:37 AM

is there actually a need to use addslashes() with your login credentials? itís not that you save them to the DB Ö
Was This Post Helpful? 0
  • +
  • -

#4 GrueKun   User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 17
  • Joined: 21-November 10

Re: Using AJAX to test MySQL login credentials: not working.

Posted 15 June 2011 - 08:42 AM

No, there probably isn't. I've got a bit of a habit of cleaning all input even if its not going to be used in a DB query.

That's all that is. :)
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4214
  • View blog
  • Posts: 13,317
  • Joined: 08-June 10

Re: Using AJAX to test MySQL login credentials: not working.

Posted 15 June 2011 - 08:45 AM

then rather use
filter_input(INPUT_POST, "uname", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH)

This post has been edited by Dormilich: 15 June 2011 - 08:46 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1