Jump to content

How is this to prevent MySQL injection?


devWhiz

Recommended Posts

$_POST['user_name'] = "CLUEL3SS";
$_POST['user_pass'] = "test123";
$_POST['confirm_pass'] = "test123";
$_POST['user_email'] = "user@email.com";
$_POST['confirm_pass'] = 'user@email.com';

function testFunc($inputVars){
	foreach($inputVars as $key=>$value){
		$escapeData[$key] = mysql_real_escape_string($value);
	}
return $escapeData;
}

var_dump(testFunc($_POST));

 

I'm trying to make a user system for my site and I want to make sure its secure enough to void off injection attackers. Any useful advice and and suggestions would be greatly appreciated!

 

Thanks!

Link to comment
Share on other sites

so something like this

 

$_POST['user_name'] = "CLUEL3SS";
$_POST['user_pass'] = "test123";
$_POST['confirm_pass'] = "yes123";
$_POST['user_email'] = "user@email.com";
$_POST['confirm_pass'] = 'user@email.com';

$userData = array_map('mysql_real_escape_string', $_POST);

print_r($userData);



Link to comment
Share on other sites

so something like this

 

$_POST['user_name'] = "CLUEL3SS";
$_POST['user_pass'] = "test123";
$_POST['confirm_pass'] = "yes123";
$_POST['user_email'] = "user@email.com";
$_POST['confirm_pass'] = 'user@email.com';

$userData = array_map('mysql_real_escape_string', $_POST);

print_r($userData);



 

This will work if you only have one DB connection. If you work with more than one DB server you should use something like:

 

$userData = array_map(function($value) use (&$db2) { return mysql_real_escape_string($value, $db2); }, $_POST);

 

$db2 being the database connection you want to use.

Link to comment
Share on other sites

so something like this

 

$_POST['user_name'] = "CLUEL3SS";
$_POST['user_pass'] = "test123";
$_POST['confirm_pass'] = "yes123";
$_POST['user_email'] = "user@email.com";
$_POST['confirm_pass'] = 'user@email.com';

$userData = array_map('mysql_real_escape_string', $_POST);

print_r($userData);



 

This will work if you only have one DB connection. If you work with more than one DB server you should use something like:

 

$userData = array_map(function($value) use (&$db2) { return mysql_real_escape_string($value, $db2); }, $_POST);

 

$db2 being the database connection you want to use.

 

Keep in mind this only works on PHP >= 5.3.0

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.