Bisa Posted October 22, 2008 Share Posted October 22, 2008 Been trying to secure my form input to prevent sql injections using this function, however, I am unable to get anything returned from it and I do not know why function secureFormInput($formdata) //Prevents sql insertion { //Removes blank spaces at the beginning and at the end of the string $formdata = trim($formdata); //If get_magic_quotes_gpc is set, removes the slashes to prepare for mysql_real_escape_string if(get_magic_quotes_gpc()) $formdata = stripslashes(formdata); //Escapes the string and returns the now secured data return mysql_real_escape_string($formdata, $db_connect); } any pointers from more experienced ppl out there is appreciated :-\ Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/ Share on other sites More sharing options...
thebadbad Posted October 22, 2008 Share Posted October 22, 2008 Maybe because the $db_connect variable is empty? Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671955 Share on other sites More sharing options...
Bisa Posted October 22, 2008 Author Share Posted October 22, 2008 I've fed the db variable with mysql_connect(*****) so I doubt that, but could I get this result if the db was offline and I am unable to connect as well? Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671957 Share on other sites More sharing options...
rhodesa Posted October 22, 2008 Share Posted October 22, 2008 if the db is not connected, you should use mysql_escape_string() Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671960 Share on other sites More sharing options...
thebadbad Posted October 22, 2008 Share Posted October 22, 2008 I was saying that, because the variable isn't global - thus empty inside the function since it's not declared there or sent along as a parameter. Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671961 Share on other sites More sharing options...
wildteen88 Posted October 22, 2008 Share Posted October 22, 2008 $db_connect wont be available to the your secureFormInput() function by default. Functions do not inherit variables set outside of it. In order for your function to use the $db_connect variable you'll need to add the following at the top of your function global $db_connect; eg function secureFormInput($formdata) //Prevents sql insertion { global $db_connect; Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671962 Share on other sites More sharing options...
rhodesa Posted October 22, 2008 Share Posted October 22, 2008 or if you only have one mysql connection, just leave the $db_connect out, as PHP will use the most recent connection Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671963 Share on other sites More sharing options...
discomatt Posted October 22, 2008 Share Posted October 22, 2008 $formdata = stripslashes(formdata); Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671972 Share on other sites More sharing options...
rhodesa Posted October 22, 2008 Share Posted October 22, 2008 $formdata = stripslashes(formdata); haha...good catch...that should be $formdata = stripslashes($formdata); Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671976 Share on other sites More sharing options...
discomatt Posted October 22, 2008 Share Posted October 22, 2008 $db_connect wont be available to the your secureFormInput() function by default. Functions do not inherit variables set outside of it. In order for your function to use the $db_connect variable you'll need to add the following at the top of your function global $db_connect; eg function secureFormInput($formdata) //Prevents sql insertion { global $db_connect; or create a database handling class! <pre><?php class db { private $link; public function __construct( $h, $u, $p, $db ) { if( ($this->link = mysql_connect($h, $u, $p)) === FALSE ) throw new Exception( 'Unable to connect to database!' ); if( mysql_select_db($db, $this->link) === FALSE ) throw new Exception( 'Unable to select database!' ); } public function query( $q ) { if( ($r = mysql_query($q)) === FALSE ) throw new Exception( 'Query failed!<br />'.mysql_error($this->link) ); else return $r; } public function clean ( $data ) { //Removes blank spaces at the beginning and at the end of the string $data = trim($data); //If get_magic_quotes_gpc is set, removes the slashes to prepare for mysql_real_escape_string if(get_magic_quotes_gpc()) $data = stripslashes($data); //Escapes the string and returns the now secured data return mysql_real_escape_string($data, $this->link); } } try { $db = new db( 'localhost', 'root', '', 'database' ); $val = "zomg ' test ' with ' escapes"; echo $val."\n"; $val = $db->clean( $val ); echo $val."\n"; } catch ( Exception $e ) { echo $e->getMessage(); } ?></pre> Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671977 Share on other sites More sharing options...
Bisa Posted October 22, 2008 Author Share Posted October 22, 2008 thnx for the quick replies, just had some food and my topic is swarmed My php programming skillz are not mature enough to start using classes, one step at a time but cheers for suggesting it. Nice catch indeed with the (formdata) indeed, thnx then about global, I'm fairly new to php but I found out early on that using globals is not a secure way of handling variables and also might differ from programmer to programmer making code hard to port to others - however, I guess using globals without user input and only in the script itself is of no harm? Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671991 Share on other sites More sharing options...
wildteen88 Posted October 22, 2008 Share Posted October 22, 2008 then about global, I'm fairly new to php but I found out early on that using globals is not a secure way of handling variables and also might differ from programmer to programmer making code hard to port to others - however, I guess using globals without user input and only in the script itself is of no harm? I think your confusing the globals keyword with register_globals. They are not the same. Defining variables as global in a function is not insecure. register_globals on the other hand is, which is why it is not recommended. Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671996 Share on other sites More sharing options...
discomatt Posted October 22, 2008 Share Posted October 22, 2008 I'd avoid using the global keyword. Instead, I'd use the $GLOBALS superglobal to make a copy of the database link identifier to use in the function <pre><?php function clean ( $data ) { // Create a copy of the global resource, this way the function can // never accidentally overwrite the variable if( !isset($GLOBALS['link']) ) return $data; $link = $GLOBALS['link']; //Removes blank spaces at the beginning and at the end of the string $data = trim($data); //If get_magic_quotes_gpc is set, removes the slashes to prepare for mysql_real_escape_string if(get_magic_quotes_gpc()) $data = stripslashes($data); //Escapes the string and returns the now secured data return mysql_real_escape_string($data, $link); } $link = mysql_connect( 'localhost', 'root', '' ); $val = "zomg ' test ' with ' escapes"; echo $val."\n"; $val = clean( $val ); echo $val."\n"; ?></pre> Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671997 Share on other sites More sharing options...
discomatt Posted October 22, 2008 Share Posted October 22, 2008 Defining variables as global in a function is not insecure. register_globals on the other hand is, which is why it is not recommended. Neither is using the 'global' keyword. By giving functions the ability to change variables on the global scope you are creating a debugging nightmare. If a function accidentally over-writes $db_connect, it'll break your whole script... and you'll have to check every function that uses $db_connect globally in order to debug the problem. Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-671998 Share on other sites More sharing options...
Bisa Posted October 22, 2008 Author Share Posted October 22, 2008 cheers, thnx for the help Link to comment https://forums.phpfreaks.com/topic/129613-solved-mysql_real_escape-function-leaves-me-with-nothing/#findComment-672141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.