Jump to content

question about mysql injection


php_novice2007

Recommended Posts

Your best using 'mysql_real_escape_string()'...

See: http://uk3.php.net/manual/en/function.mysql-real-escape-string.php

But as someone pointed out earlier, the '`' character is not removed but throws an error. So i've now put the following before the escape check:

function get_REQUEST($name)
{
$sret = "";

if (isset($_REQUEST[$name]))
{
	$sret = $_REQUEST[$name];
	$sret = str_replace("`","'", $sret);
	$sret = mysql_real_escape_string($sret);	//	check for injection attacks
}

return $sret;
}

Link to comment
Share on other sites

Hi,

 

I've got something like this, is that the same as what you've got?

 

$userid = $_POST['login'];
$passWord = $_POST['password'];

require("databaseInfo.php");
$dbtable = "users";
$link=mysql_connect("localhost", $username, $password) or die("Cannot connect to database");
  
//select database
@mysql_select_db($database) or die("Unable to select database");

if(get_magic_quotes_gpc()) {
      $userid = stripslashes($userid);
      $passWord = stripslashes($passWord);
} 
    
$query = sprintf("SELECT * FROM %s WHERE user_id = '%s'", $dbtable, mysql_real_escape_string($userid, $link));
$result=mysql_query($query, $link) or die("Unable to load selected table");

 

I think I copied the code from somewhere so not really sure what the magic_quotes_gpc do.. Do I still need your code to replace " ` " with " ' "?

 

Thanks! 

   

Link to comment
Share on other sites

no it can but the characters are escaped so in the case of ' it will become \'

 

So im guessing you know that with the backslash being there that the ' will be seen as a literal character and not a special one.

 

Also, can backticks actually be used for sql injection ???

And wouldn't it be better to use this:

 

function get_REQUEST($name)
{
$sret = "";

if (isset($_REQUEST[$name]))
{
	$sret = $_REQUEST[$name];
	$sret = str_replace("`","\`", $sret);
	$sret = mysql_real_escape_string($sret); // check for injection attacks
}

return $sret;
}

 

That way you are escaping the backtick without changing its value

 

~ Chocopi

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.