Jump to content

Recommended Posts

Hmmm... there are several ways to prevent an SQL injection... but, is there a more efficient ways than this:

 

$formData=$_POST['formData'];

$formData=str_replace("'","'",$formData);

$formData=str_replace("\"",""",$formData);

$formData=str_replace("<","&#60;",$formData);

$formData=str_replace(">","&#62;",$formData);

$formData=str_replace("$","&#36;",$formData);

 

 

I'm looking for a shorter method than this. I want to block potential HTML, database, and server-side hacks.

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/53828-php-sql-injection/
Share on other sites

Try this:

<?php
if (get_magic_quotes_gpc()) {
	$_POST = array_map('stripslashesinarray', $_POST);
	$_GET = array_map('stripslashesinarray', $_GET);
	$_COOKIE = array_map('stripslashesinarray', $_COOKIE);
	$_REQUEST = array_map('stripslashesinarray', $_REQUEST);
}

function stripslashesinarray($value) {
	return (is_array($value) ? array_map('stripslashesinarray', $value):stripslashes($value));
}

function escape($value) {
	return ($value == '') ? "''":is_string($value) ? "'".addslashes($value)."'":$value;
}

$sql = "INSERT INTO `table` SET `column` = '".escape($_POST['column'])."';";
?>

Link to comment
https://forums.phpfreaks.com/topic/53828-php-sql-injection/#findComment-266129
Share on other sites

I doubt it handles the html characters, but it will prevent SQL Injection, not against XSS Exploits.

 

But you can use www.php.net/htmlentities I believe to help with that or www.php.net/strip_tags or you can even create your own:

 

www.php.net/str_replace

<?php
$replace = array("&", "<", ">");
$with = array("&", "<", ">");
$content = "<html> test  & this & that </html>";

$content = str_replace($replace, $with, $content);
?>

 

Which in return renders just about all html tags and javascript tags useless.

Link to comment
https://forums.phpfreaks.com/topic/53828-php-sql-injection/#findComment-266494
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.