HDFilmMaker2112 Posted June 5, 2011 Share Posted June 5, 2011 First time I'm looking into creating my own functions, so I'm making sure I'm getting this right: If I have the following: <?php function formSanitize($formValue){ $formValue = stripslashes($formValue); $formValue = mysql_real_escape_string($formValue); } ?> Then if I just use this: <?php $product_name=formSanitize($_POST['product_name']); ?> It should do the same this as this: $product_name=$_POST['product_name']; $product_name = stripslashes($product_name); $product_name = mysql_real_escape_string($product_name); Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/ Share on other sites More sharing options...
KevinM1 Posted June 5, 2011 Share Posted June 5, 2011 Don't forget to return your result from your function. http://us2.php.net/manual/en/functions.returning-values.php Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225648 Share on other sites More sharing options...
xyph Posted June 5, 2011 Share Posted June 5, 2011 Your function needs to RETURN a value if you want to use it to define. function formSanitize($formValue){ $formValue = stripslashes($formValue); $formValue = mysql_real_escape_string($formValue); return $formValue } Or, in one line function formSanitize($formValue){ return mysql_real_escape_string( stripslashes($formValue) ); } Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225649 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 5, 2011 Author Share Posted June 5, 2011 Alright, thanks. One more question... should the function go before or after it's used? I'm assuming before, but double checking. Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225651 Share on other sites More sharing options...
xyph Posted June 5, 2011 Share Posted June 5, 2011 Doesn't matter. Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225656 Share on other sites More sharing options...
Pikachu2000 Posted June 5, 2011 Share Posted June 5, 2011 You shouldn't apply stripslashes() without first checking to see if magic_quotes_gpc() is enabled. It will strip slashes out that are actually supposed to be there if you use it in that manner. function formSanitize($formValue){ if( function_exists(get_magic_quotes_gpc()) && get_magic_quotes_gpc() ) { $formValue = stripslashes($formValue); } $formValue = mysql_real_escape_string($formValue); return $formValue; } Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225664 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 6, 2011 Author Share Posted June 6, 2011 You shouldn't apply stripslashes() without first checking to see if magic_quotes_gpc() is enabled. It will strip slashes out that are actually supposed to be there if you use it in that manner. function formSanitize($formValue){ if( function_exists(get_magic_quotes_gpc()) && get_magic_quotes_gpc() ) { $formValue = stripslashes($formValue); } $formValue = mysql_real_escape_string($formValue); return $formValue; } If I do that I get this: Is your name O\'reilly? abssagasdfasdf \' \ asdfjkla\\ asdala\? From this: <?php function sanitize($formValue){ if( function_exists(get_magic_quotes_gpc()) && get_magic_quotes_gpc() ) { $formValue = stripslashes($formValue); } //$formValue = mysql_real_escape_string($formValue); return $formValue; } $string="Is your name O\'reilly?"; $string=sanitize($string); echo $string; $string2="abssagasdfasdf \' \ asdfjkla\\\ asdala\?"; $string2=sanitize($string2); echo "<br />"; echo $string2; ?> It's only removing one slash. Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225682 Share on other sites More sharing options...
xyph Posted June 6, 2011 Share Posted June 6, 2011 You don't want it to remove slashes that were entered intentionally though. You want to remove slashes that were automatically added. Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225685 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 6, 2011 Author Share Posted June 6, 2011 But once mysql_real_escape_string($formValue); is used, wouldn't that add another backslash to "O\'reilly" resulting in "O\\'reilly"? Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225688 Share on other sites More sharing options...
xyph Posted June 6, 2011 Share Posted June 6, 2011 When you grab the data out of the database, you will get "O\'reilly" which is what your original input is. What's the issue? PHP will only turn "O'reilly" into "O\'reilly" if get_magic_quotes_gpc() returns 1. Your script checks for this Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225691 Share on other sites More sharing options...
KevinM1 Posted June 6, 2011 Share Posted June 6, 2011 The escape only lasts until the data is inserted into the db. It's removed after insertion, so, like xyph said, you won't see an extra slash when retrieving the data back from the db. Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225693 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 6, 2011 Author Share Posted June 6, 2011 Alright made a test table and test code: <?php function sanitize($formValue){ if( function_exists(get_magic_quotes_gpc()) && get_magic_quotes_gpc() ) { $formValue = stripslashes($formValue); } $formValue = mysql_real_escape_string($formValue); return $formValue; } $string="Is your name O\'reilly?"; $string=sanitize($string); $sql="INSERT INTO $tbl_name (test123) VALUES ('$string')"; mysql_query($sql) or die("Problem with the query: $sql<br />" . mysql_error()); echo "Inserted: $string <br /><br />"; $sql2="SELECT * FROM $tbl_name"; $result2=mysql_query($sql2); while($row2=mysql_fetch_array($result2)){ extract($row2); echo $test123; } ?> That echo's out: Inserted: Is your name O\\\'reilly? Is your name O\'reilly? So how would I remove the \ from the data pulled from database? Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225705 Share on other sites More sharing options...
jcbones Posted June 6, 2011 Share Posted June 6, 2011 You wouldn't put the escape into the string when typing it in, would you? So the string should be: $string = "Is your name 0'reilly?"; Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225706 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 6, 2011 Author Share Posted June 6, 2011 You wouldn't put the escape into the string when typing it in, would you? So the string should be: $string = "Is your name 0'reilly?"; The issue is, what I'm coding will eventually be out for other people to use; It's cart software. They might think they have to escape stuff when they type it in the admin panel. Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225707 Share on other sites More sharing options...
jcbones Posted June 6, 2011 Share Posted June 6, 2011 Well, how do you propose removing one's they *think* they need, vs ones the *know* they want? Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225711 Share on other sites More sharing options...
KevinM1 Posted June 6, 2011 Share Posted June 6, 2011 You wouldn't put the escape into the string when typing it in, would you? So the string should be: $string = "Is your name 0'reilly?"; The issue is, what I'm coding will eventually be out for other people to use; It's cart software. They might think they have to escape stuff when they type it in the admin panel. Why would your users think that? There are really only two scenarios: 1. Your users don't know what escaping is. 2. Your users expect the software will do the escaping for them, which is what professional software should do. The thought of an end user deciding to manually escape their own data is ridiculous. Quote Link to comment https://forums.phpfreaks.com/topic/238513-creating-functions/#findComment-1225747 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.