tags:

views:

32

answers:

1

I wrote a code for login verification..I got output with GET. But i need output with POST since it is more secure.pls let me know if there is any error in my code.

javascript code:

var xml;
function verifyusernamepasswd(pass)
{
//pass is password that will be passed as parameter
xml=new XMLHttpRequest();
var url="http://localhost/loginvalidate.php";
var para="q="+username+"&p="+pass;//username is global
xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xml.setRequestHeader("Content-length", para.length);
xml.setRequestHeader("Connection", "close");
xml.open("POST",url,true);
xml.onreadystatechange=statechanged1;
xml.send(para);

}

function statechanged1()
{
if(xml.readyState==4)
alert(xml.responseText);

}


php code:

<?php
$username=$_POST["q"];
$password=$_POST["p"];
$con=mysql_connect("localhost","root","blaze");
if(!$con)
{
die('Could not connect: '.mysql.error());
}
mysql_select_db("BLAZE",$con) or die("No such Db");
$result=mysql_query("SELECT Passwword FROM USERTABLE WHERE Userhandle='$username'");
if($result==null)
echo "false";

else if($result!=null)
{
$row=mysql_fetch_array($result);
if((strcmp($row['Passwword'],$password)==0))
echo "true";
else
echo "false";
}

?>

the verification does not return anything, cos my alert is not displayed at all...pls tell me whats wrong....

A: 

Have you tried accessing the PHP page directly to see if there are any errors? You might also want to try using Firebug (for Firefox), or the developer tools in Chrome to make sure that the request is actually being sent, and inspect the result.

Is Passwword spelt that way in the database, or is it just a typo in your PHP?

Finally, while it won't cause you problems now, it might down the line: your code is vulnerable to SQL injection - a malicious user could pass some bad arguments and do nasty things to your database. You need to escape $username before using it in a SQL query, e.g.:

"SELECT ... WHERE userhandle = '" . mysql_real_escape_string($username) . "'"
Chris Smith
yes passwword is spelt that way in my db:-)it was a spelling error...my php code is correct. i ran it in local host and it gave correct outputs...and thank you for the note on sql injection..it will be helpful...
Neethusha
Another thing is i tried echoing the value of $username n then $password in the php code in beginning. there was no alert..so i think the parameters r not reaching the php code at all..
Neethusha
Are you sure the browser is sending the request? Use Firebug/chrome dev tools/etc. Try alerting the value of readyState even if it's not 4, etc.
Chris Smith