Jump to content

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
https://forums.phpfreaks.com/topic/256354-how-is-this-to-prevent-mysql-injection/
Share on other sites

If you just want to perform the same operation on all elements of an array, you can use array_map. Beyond that, since all those values are strings, and you use mysql_real_escape_string(), you should be fine.

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);



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.

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

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.