String Cleaning Function

Place to place any code snippets, completed games, or even uncompleted games for IR users to use.
Post Reply
User avatar
Gate
Posts: 25
Joined: Tue Jan 25, 2011 2:35 am

String Cleaning Function

Post by Gate »

Make a function in your common includes file as such:

Code: Select all

<?php
//////////////////////////////////////////////////////
// Function: Secure_Input()
// Use: Secures input against SQL/Script injection
//////////////////////////////////////////////////////
function secure_input($input){
     $input = htmlentities($input);
     $input = mysql_real_escape_string($input);
     $input = strip_tags($input);
     return $input;
}
//////////////////////////////////////////////////////
// End - Secure_Input()
//////////////////////////////////////////////////////
?>
Example Code:

Code: Select all

<?php

$variable = $_POST['variable'];
$clean_variable = secure_input($variable);

?>
Last edited by Gate on Thu Feb 03, 2011 7:21 am, edited 1 time in total.
My software never has bugs. It just develops random features.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: String Cleaning Function

Post by Jackolantern »

Don't you need to either pass the parameter by reference, such as:

Code: Select all

function secure_input(&$input){  //added pass-by-reference symbol
     $input = htmlentities($input);
     $input = mysql_real_escape_string($input);
     $input = strip_tags($input);
}
...or return the value?

Code: Select all

function secure_input($input){
     $input = htmlentities($input);
     $input = mysql_real_escape_string($input);
     $input = strip_tags($input);
     return $input;
}
Otherwise $input will be a local copied-by-value variable and will simply go out of scope when the function returns without changing the underlying value of the variable passed into the function call.
The indelible lord of tl;dr
User avatar
Gate
Posts: 25
Joined: Tue Jan 25, 2011 2:35 am

Re: String Cleaning Function

Post by Gate »

Jackolantern wrote:Don't you need to either pass the parameter by reference, such as:

Code: Select all

function secure_input(&$input){  //added pass-by-reference symbol
     $input = htmlentities($input);
     $input = mysql_real_escape_string($input);
     $input = strip_tags($input);
}
I've only ever seen the passing of parameters by reference in a class, never seen much use of them in general and never seen there use in a professional web project, so i'll stick to my way for now ;)
Jackolantern wrote: ...or return the value?

Code: Select all

function secure_input($input){
     $input = htmlentities($input);
     $input = mysql_real_escape_string($input);
     $input = strip_tags($input);
     return $input;
}
Otherwise $input will be a local copied-by-value variable and will simply go out of scope when the function returns without changing the underlying value of the variable passed into the function call.
You don't actually need to return the value in the function for this code to work if the function is used as defined below, the variable does not lose scope.

Code: Select all

$input = secure_input($input);
although it is good practise to always have the return command in there, so thanks for pointing it out, edited the code to reflect the changes
My software never has bugs. It just develops random features.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: String Cleaning Function

Post by Jackolantern »

Gate wrote:
Jackolantern wrote:Don't you need to either pass the parameter by reference, such as:

Code: Select all

function secure_input(&$input){  //added pass-by-reference symbol
     $input = htmlentities($input);
     $input = mysql_real_escape_string($input);
     $input = strip_tags($input);
}
I've only ever seen the passing of parameters by reference in a class, never seen much use of them in general and never seen there use in a professional web project, so i'll stick to my way for now ;)
You are right. Passing by reference is typically considered bad practice and should only be used for optimization, or when no other option exists, since it can introduce difficult-to-track bugs into the application.
Gate wrote:
Jackolantern wrote: ...or return the value?

Code: Select all

function secure_input($input){
     $input = htmlentities($input);
     $input = mysql_real_escape_string($input);
     $input = strip_tags($input);
     return $input;
}
Otherwise $input will be a local copied-by-value variable and will simply go out of scope when the function returns without changing the underlying value of the variable passed into the function call.
You don't actually need to return the value in the function for this code to work if the function is used as defined below, the variable does not lose scope.

Code: Select all

$input = secure_input($input);
although it is good practise to always have the return command in there, so thanks for pointing it out, edited the code to reflect the changes
Hmm...I was curious about this since the PHP Manual says that the default behavior was pass-by-value. I tried out a function that simply re-assigned the string value once it was passed in, used the same variable name, and it didn't work. In fact, it didn't even have the initial, out-of-function value when I echo'd it, which was very odd. PHP has some really strange bits to it... lol
The indelible lord of tl;dr
User avatar
Gate
Posts: 25
Joined: Tue Jan 25, 2011 2:35 am

Re: String Cleaning Function

Post by Gate »

It seem's that the passing by reference symbols are PHP version's of pointers, where as pointer's in C++ actually have a use in referencing blocks of memory, PHP it seems to me, lacks any use with it's 'pointer' system in a web enviroment
My software never has bugs. It just develops random features.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: String Cleaning Function

Post by Jackolantern »

Yeah, it is just a hold-over from C/C++ for passing-by-reference. Most modern languages use the keyword "ref" in front of the parameter to signal a pass-by-reference rather than use the old C++ "&" symbol. As you mentioned, PHP does not have true pointer arithmetic or functionality, so it is kind of silly that they used it (particularly since it was added after Java changed the industry standard to using the "ref" keyword).
The indelible lord of tl;dr
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: String Cleaning Function

Post by Chris »

Memory saver:

Code: Select all

function secure_input($input)
{
     return htmlentities(mysql_real_escape_string(strip_tags($input)));
}
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Gate
Posts: 25
Joined: Tue Jan 25, 2011 2:35 am

Re: String Cleaning Function

Post by Gate »

Chris wrote:Memory saver:

Code: Select all

function secure_input($input)
{
     return htmlentities(mysql_real_escape_string(strip_tags($input)));
}
 
Rather go for clarity,good formatting and comments over saving maybe 2 bytes of memory. you should too!
My software never has bugs. It just develops random features.
Post Reply

Return to “Code Sharing”