PDA

View Full Version : adding php function


FrEaKmAn
February 9th, 2007, 01:19 PM
Hi

So I have multiple inputs, let's say name, surname and email. Now I want to apply functions to this, like to add slashes. How can I easily do this, let me remember you that I have over 15 inputs on one page, so I don't want to have along list..

Buzz
February 9th, 2007, 05:39 PM
On the page, before the bottom ?> tag just add this.


function input_string($string) {
$string = trim($string);
$string = addslashes($string);
$string = str_replace('<', '&lt;', $string);
$string = str_replace('>', '&gt;', $string);
return $string;
}

function output_string($string){
$string = stripslashes($string);
$string = str_replace('&lt;', '<', $string);
$string = str_replace('&gt;', '>', $string);
return $string;
}


Before inserting variables in the database set them up like this:


$name = input_string($_POST['name']);
$address = input_string($_POST['address']);
$number = input_string($_POST['number']);


When pulling data from the database and displaying it use this:


$name = output_string($row['name']);
$address = output_string($row['address']);
$number = output_string($row['number']);



That will pass the variables through the functions and prepare them for insertion or display ensuring that no data which can be harmful to the database gets inserted into it.