latheesan
02-13-2006, 12:41 PM
Hello,
I came across this page http://us2.php.net/manual/en/function.mysql-real-escape-string.php (Example 3) about how to prevent sql inject.
So i constructed my own script like this:
<?php
include("../vars.php");
function safe($query)
{
if (get_magic_quotes_gpc())
{
$query = stripslashes($query);
}
if (!is_numeric($query))
{
$query = "'" . mysql_real_escape_string($query) . "'";
}
return $query;
}
mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name);
if($_POST['cmd'] == "Login")
{
$query = sprintf("SELECT * FROM admin_config WHERE username=%s AND password=%s",
safe($_POST['username']),
md5($_POST['password']));
$result = mysql_query($query);
if(mysql_num_rows($result) == 1)
{
echo "Correct Login";
} else {
echo "Wrong Login";
}
}
mysql_close();
?>
<form action="test.php" method="post">
<table border="0" width="100%">
<tr>
<td><input type="text" size="20" name="username"></td>
</tr>
<tr>
<td><input type="text" size="20" name="password"></td>
</tr>
<tr>
<td><input type="submit" name="cmd" value="Login"></td>
</tr>
</table>
</form>
When i execute the test.php script, no errors, but when i enter the correct login details press login button, i get the following message:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\server\xampp\htdocs\test.php on line 29
Wrong Login
How can I get it to work?
Are there any DB Query Class out there with Anti-SQL Inject feature?
Thanks in advance for your help.
I came across this page http://us2.php.net/manual/en/function.mysql-real-escape-string.php (Example 3) about how to prevent sql inject.
So i constructed my own script like this:
<?php
include("../vars.php");
function safe($query)
{
if (get_magic_quotes_gpc())
{
$query = stripslashes($query);
}
if (!is_numeric($query))
{
$query = "'" . mysql_real_escape_string($query) . "'";
}
return $query;
}
mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
mysql_select_db($db_name);
if($_POST['cmd'] == "Login")
{
$query = sprintf("SELECT * FROM admin_config WHERE username=%s AND password=%s",
safe($_POST['username']),
md5($_POST['password']));
$result = mysql_query($query);
if(mysql_num_rows($result) == 1)
{
echo "Correct Login";
} else {
echo "Wrong Login";
}
}
mysql_close();
?>
<form action="test.php" method="post">
<table border="0" width="100%">
<tr>
<td><input type="text" size="20" name="username"></td>
</tr>
<tr>
<td><input type="text" size="20" name="password"></td>
</tr>
<tr>
<td><input type="submit" name="cmd" value="Login"></td>
</tr>
</table>
</form>
When i execute the test.php script, no errors, but when i enter the correct login details press login button, i get the following message:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\server\xampp\htdocs\test.php on line 29
Wrong Login
How can I get it to work?
Are there any DB Query Class out there with Anti-SQL Inject feature?
Thanks in advance for your help.
