Page 1 of 1

$_GET & $_POST Question

Posted: Wed Aug 16, 2006 5:14 am
by WorldCom
Ok, I have a page that gets called from a <FORM> and also a link <A HREF='page.php?var=$var'>.

So for the variables I need $_GET ... and $_POST for the form.

Is it correct to put something like this:

Code: Select all

$var = $_GET['var'];
$var = $_POST['var'];
so it picks up the variable with either type of input, or should I use some kind of if statement to filter it first?
Note that I do filter the input to make sure nothing nasty comes through.

Posted: Wed Aug 16, 2006 5:28 am
by JayBird

Code: Select all

if(!empty($_POST))
    $var = $_POST['var']; 
else
    $var = $_GET['var'];

Posted: Wed Aug 16, 2006 5:32 am
by WorldCom
So simple ..... thanks :)

Posted: Wed Aug 16, 2006 5:38 am
by Jenk
It would be best to specify the indice required explicitly, and optional default value:

Code: Select all

if (!empty($_POST['var'])) {
    $var = $_POST['var'];
} elseif (!empty($_GET['var'])) {
    $var = $_GET['var'];
} else {
    $var = 'default';
}
If a different value was available in $_POST/$_GET you would get a 'false' positive :)

Posted: Wed Aug 16, 2006 8:15 am
by feyd
I'd make that a function. Image