My code used to be a whole mess when submitting back to complex pages, now I have functions like this that I either define at the bottom of the page, or in a functions script
function fill_user_data()
{
global $userData, $dbData;
$fields = array('title','name','surname','address','address2','town','county','postcode','tel','mobile',
'username','password','email','gender','dobday','dobmonth','dobyear','paypal','chequename');
if ( get_magic_quotes_gpc() )
foreach ($fields as $field)
$_REQUEST[$field] = @stripslashes($_REQUEST[$field]);
foreach ($fields as $field)
{
$dbData[$field] = '';
if (isset($_REQUEST[$field]))
$dbData[$field] = mysql_escape_string(trim($_REQUEST[$field]));
$userData[$field] = htmlspecialchars($_REQUEST[$field]);
}
}
It's called at the top of the script, then any input fields have <?= userData['name'] ?> etc in them that's already cleaned, stripped, and ready to go to the database if they're all valid - and if an extra field is needed then you just add it to the array, loads of my code is based on names in arrays, and I wish I'd thought of it a lot earlier than I did. I've recently written a whole thing to handle request that deals with everything for me, it's simple but way effective.
I can't be bothered to attach it, so I'll paste - if anyone wants me to nuke it say so and I will
/* Class to handle stripping, escaping, trimming and field checking of request variables
*
* This will be a beauty for handling and verifying input
*
* funcs strip, trim, specialchars, escape, reset, verify
*/
class RequestHandler
{
var $data; // copy of request data, auto de-slashed if need be
var $reqFilled; // array list of fields that have to be filled when verify is called
var $reqNumeric; // array of fields that have to be numeric
var $reqMinLen; // associative array of $key => minimum lengths
var $errors; // array of error messages
function RequestHandler()
{
$this->data = $_REQUEST;
$this->errors = array();
$this->reqFilled = array();
$this->reqNumeric = array();
$this->reqMinLen = array();
if (get_magic_quotes_gpc())
$this->strip();
}
function strip()
{
$this->recurse_it($this->data, 0, 'stripslashes');
}
function trim()
{
$this->recurse_it($this->data, 0, 'trim');
}
function specialchars()
{
$this->recurse_it($this->data, 0, 'htmlspecialchars');
}
function escape()
{
$this->recurse_it($this->data, 0, 'mysql_escape_string');
}
// hope you appreciate how lovely this is
function recurse_it(&$in, $dead, $function)
{
if ( is_array($in) )
array_walk($in, array($this, 'recurse_it'), $function);
else
$in = $function($in);
}
function reset()
{
$this->RequestHandler();
}
// returns true or false dependent on the status of required fields
function verify()
{
$errCnt = 0;
foreach ($this->reqFilled as $key)
{ if (empty($this->data[$key]))
{
$this->errors[] = 'field "' . $key. '" empty';
$errCnt++;
}
}
foreach ($this->reqNumeric as $key)
{ if (!isset($this->data[$key]) || !is_numeric($this->data[$key]))
{
$this->errors[] = 'field "' . $key. '" not numeric';
$errCnt++;
}
}
foreach ($this->reqMinLen as $field => $len)
{ if (!isset($this->data[field]) || strlen($this->data[$field]) < $len)
{
$this->errors[] = 'field "' . $field . '" less than ' . $len . ' chars';
$errCnt++;
}
}
return $errCnt ? false : true;
}
// takes array of field names that must be filled
function set_req_filled($rf)
{
$this->reqFilled = $rf;
}
// takes array of fields that must be numeric
function set_req_numeric($rn)
{
$this->reqNumeric = $rn;
}
// takes associative array of field names and their min length
function set_req_minimum($rm)
{
$this->reqMinLen = $rm;
}
}