0

What is the best and method of ensuring that $page and $getVars are clean and safe?

//fetch the passed request
$request = $_SERVER['QUERY_STRING'];

//parse the page request and other GET variables
$parsed = explode('&', $request);

//the page is the first element
$page = array_shift($parsed);

//the rest of the array are GET statements, parse them out;
$getVars = array();
foreach($parsed as $argument)
{
  //split GET vars along '=' symbol to seperate the variable and its value
  list($variable, $value) = explode('=', $argument);
  $getVars[$variable] = $value;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
davivid
  • 5,910
  • 11
  • 42
  • 71

1 Answers1

0

In your case you just want to use $_GET or $_REQUEST instead of $_SERVER['QUERY_STRING'] as the others already have mentioned.

In order to verify and clean your variables, you should have a look at PHP's Data Filtering functions which were introduced in PHP 5.2. You also find some examples in the PHP manual similar to this:

if (filter_var('0.0.0.0', FILTER_VALIDATE_IP) !== false) {
    // IP address is valid
}

or

$options = array(
    'options' => array(
        'min_range' => 0,
        'max_range' => 2,
    )
);

if (filter_var(1, FILTER_VALIDATE_INT, $options) !== false) {
    // 1 is between 0 and 2
}
steffen
  • 16,138
  • 4
  • 42
  • 81