Jump to content

SQL Injection - Loop though post variables to get em!


scotchegg78

Recommended Posts

Hi Guys

 

I have this which works fine...

 

foreach ($_POST as $key => $input_arr) {
    $_POST[$key] = mysqli_real_escape_string($this->db_link,addslashes($input_arr));
	}

 

but it fails for $_POST variables that are arrays, so i have tried this approach, what am i doing wrong ?

 

foreach ($_POST as $key => $input_arr) {
		if(is_array($_POST)){
			foreach($_POST[$key] as $key2 => $input_arr){

				$_POST[$key][$key2] = mysqli_real_escape_string($this->db_link,addslashes($input_arr));

			}
		}else
		{
		   	$_POST[$key] = mysqli_real_escape_string($this->db_link,addslashes($input_arr));
		}
	}

 

Thanks for any help :)

You've used $input_arr twice. This should work:

 

<?php

foreach ($_POST as $key => $input_arr) {
if(is_array($input_arr)){
	foreach($input_arr as $key2 => $val){
		$_POST[$key][$key2] = mysqli_real_escape_string($this->db_link,addslashes($val));
	}
}else{
	$_POST[$key] = mysqli_real_escape_string($this->db_link,addslashes($input_arr));
}
}

?>

 

Although I still don't understand why you're using both addslashes() and real_escape_string() on the inputs- that's escaping some characters twice. I hope you're not with magic_quotes enabled as well, because in that case you're escaping the input three times..

 

Orio.

Guys

 

somehting I just noticed, I was using my function above, but then after that doing this..

 

 $this->description =  filter_input(INPUT_POST,"description",FILTER_SANITIZE_STRING); 

 

The value now in my variable is not the result of my sql inection funciton, its back to the header post ! Of course I can get around it using..

 

$this->description = filter_var($_POST['description'],FILTER_SANITIZE_STRING);

 

Unless anyone has any better cleaner ideas?

 

cheers

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.