Jump to content

MySQL Class


Wolphie

Recommended Posts

Hey,

 

I'm in the middle of working on an in-depth MySQL class to ease to process of MySQL functions and error handing.

My main question is, what would be the best way to secure the query? Without having to secure each individual item that's inserted.

 

I thought about making a function to loop through a POST array, and then sanitize the items.

 

Is there any better way?

 

 

	function sanitize($str) {

	if(!get_magic_quotes_gpc()) {
		foreach($str as $key => $value) {
			$str[$key] = mysql_real_escape_string(htmlspecialchars(htmlentities($value)));
		}
		return $str;
	}
	return false;

}

Link to comment
https://forums.phpfreaks.com/topic/101607-mysql-class/
Share on other sites

That's pretty much how I do it as well.

<?php
function myEscape($string) {
dbconnect();
$new = get_magic_quotes_gpc() ? stripslashes($string) : $string;
$safe = mysql_real_escape_string($new);
dbclose();
return $safe;
}

foreach ($_POST as $key => $val) {
$_POST[$key] = myEscape($val);
}
?>

Link to comment
https://forums.phpfreaks.com/topic/101607-mysql-class/#findComment-519844
Share on other sites

I've updated my code, so if theres any "unique" fields such as a field allowing HTML or something similar, it can be ignored.

I also intergrated some of your code, hope you don't mind.

 

	function sanitize($str, $spec = '') {

	if(!get_magic_quotes_gpc()) {

		if(!empty($spec)) {

			foreach($spec as $array)
				$spec = $array;

		}	
		foreach($str as $key => $value) {

			if($key == $spec)
				return false;
			else {
				$new = get_magic_quotes_gpc() ? stripslashes($value) : $value;
				$new = htmlspecialchars($new); $new = htmlentities($new);
				$safe = mysql_real_escape_string($new);
				$str[$key] = $safe;
			}		

		}
		return $str;
	}

}

Link to comment
https://forums.phpfreaks.com/topic/101607-mysql-class/#findComment-519852
Share on other sites

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.