Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "properly" handle $_GET variables in PHP?

Currently, I have the following code:

if(isset($_GET['mid']) && !empty($_GET['mid'])) {
    $mid = $_GET['mid'];

    if(is_numeric($mid) && $mid > 0) {
        if(isset($_GET['op']) && !empty($_GET['op'])) {
            $op = $_GET['op'];

            if($op == 'info') {
            }

            if($op == 'cast') {
            }
        }
    }
}

But I think it's too "complex" with if statements inside if statements, etc...

Would you handle it differently? How would you make it simpler?

like image 484
rfgamaral Avatar asked Dec 08 '10 14:12

rfgamaral


1 Answers

I would use filter_input with the FILTER_SANITIZE_NUMBER_FLOAT filter for mid. Something like that :

$mid = filter_input(INPUT_GET, 'mid', FILTER_SANITIZE_NUMBER_FLOAT);
$op = filter_input(INPUT_GET, 'op');
if($mid > 0){
  switch($op){
    case 'info':
      // Some code
      break;
    case 'cast':
      // Some more code
      break;
    default:
      break;
  }
}
like image 131
Arkh Avatar answered Sep 20 '22 01:09

Arkh