compruebe si los parámetros $ _GET son correctos

I have three $_GET variables and I want to check if they are correctly being passed.

$amnt = $_GET['amnt'];
$from = $_GET['from'];
$to = $_GET['to'];

if ($_GET != 'amnt' || $_GET != 'from' || $_GET != 'to' ) {
    $errorno = 1100;
    echo $errorno;

I have tried this so far, however this just checks the parameters inside the $_GET rather than the acutal amnt=&from= etc. As someone could put in ammount=&frommm=

preguntado el 02 de febrero de 12 a las 10:02

Why do you care more about the names than the actual values? -

You need to write a test that checks if each parameter contains an acceptable value. We can't tell you how to do that since you haven't said what values are considered acceptable. -

I already have an if statment in place to check if the values are null as well as decimal places etc -

So what do you want this to accomplish exactly? -

Firstly, your code will always result in a fail condition because you are comparing an array with strings and they will never be equal. Secondly, all you need to do is if (!isset($_GET['amnt'], $_GET['from'], $_GET['to'])) { -

1 Respuestas

You should just check if a certain parameter you're interested in is set and if so, use it. Like:

if (isset($_GET['from'])) {
    // use $_GET['from']

If you require a certain number of parameters to be set, check for them all:

if (isset($_GET['from'], $_GET['to'], $_GET['amnt'])) ...

If any other parameters are set or if the parameters are misspelled, ignore them. If you really want to check that no other parameters are set, you can do so like so:

if (array_diff_key($_GET, array_flip(array('amnt', 'from', 'to')))) {
    // some other keys are set!!

But what's the point, really? If you just ignore those parameters, who cares? And additional parameters can be useful for debugging, like If you check too strictly, you're only creating problems for yourself later.

Respondido 02 Feb 12, 14:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.