Detect unknown query string parameter?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Detect unknown query string parameter?

Post by JAB Creations »

I have various queries sent through anchors to set various cookies. Below is an example of how I am detecting if a value of a query property is incorrect (so that I can alert the user in example)...

Code: Select all

if (isset($_GET['audio'])) {
 if ($_GET['audio'] != "0" && $_GET['audio'] != "1") 
 {include("includes-body-prompt-15-uri-get-catcher_audio.php");}
}
What my questions is: how do I detect if a query property itself is not among the existing properties that I use? I'm not sure I'm using the correct terminology so let me do a small visual...

index.php?property=value

The italicized part is what I'm referring to as the property. So how do I detect if a non-existent property is requested so I can handle the error? Thanks!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

$expected = 'property';

if (empty($_GET[$expected]))  {} 

or (!array_key_exists($expected, $_GET)) { }
If thats not what you meant, then your going to have to rephrase the question :wink:

P.S. I've noticed you took my advise on the last thread, however you should know its good etiquette to let us know if the solution worked or not so others may benefit as well.
Last edited by John Cartwright on Thu May 03, 2007 10:40 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

foreach() + switch() + default case = :)
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

I'm not expecting a property to be requested...I'm just looking to catch potential non-existent properties.

Code: Select all

if (empty($_GET[$expected]))
Would this basically always return true even if there was no property (as in no potential for this error)? I want to make sure I do not start saying people have errors if no properties are requested. So it's not just detecting against existing properties. I'm not sure if I'm expressing this idea clearly...

No Error: index.php
No Error: index.php?audio=1
Yes Error: index.php?doesnotexist=whatever

Feyd...gah, I have no clue what to do with that syntax. :? Thanks for both your replies...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

So you want to catch any parameters that shouldn't exist? It shouldn't really matter if the user decides to set it, however.. I believe this is what feyd suggested.

Code: Select all

foreach ($_GET as $key => $value)
{
   switch ($key) 
   {
      case 'audio' :
      case 'someothervar' :
      default: 
         echo 'if it got here, some var was set that shouldn\'t exist';
   }
}
However, it could be simplified by supplying an array of expected values and using !array_key_exists()
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Let me try this angle: if $_GET is method request detect (get/post/delete..etc) or does it specifically handle these sort of requests/queries? Maybe I'm not understanding $_GET's ultimate purpose?

Because if it specifically handles these sort of queries then wouldn't I be able to compare it against an array? I'm totally lost on the for each, what is $value for? I see nothing referring to it.

The code you posted is always returning true (always reports an error).
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Let me try this angle: if $_GET is method request detect (get/post/delete..etc) or does it specifically handle these sort of requests/queries? Maybe I'm not understanding $_GET's ultimate purpose?
It is purely a matter of personal preference, although actions are usually put in the url because it is visible to the user. I could write an application that transmits solely using post, but it limits the usability of the site because the user wouldn't know what they are at/doing.
I'm totally lost on the for each, what is $value for? I see nothing referring to it.
Its just so you can get the key of the array element, otherwise you would only get the value. It is identical to doing the following, except for some reason if you needed the value it wouldn't exist anymore.

Code: Select all

foreach (array_keys($_GET) as $key)
{
   switch ($key)
   {
      case 'audio' : break;
      case 'someothervar' : break;
      default:
         echo 'if it got here, some var was set that shouldn\'t exist';
   }
}
The code you posted is always returning true (always reports an error).
I forgot to add the breaks in there. See above.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Heh, not sure what I would do with the value, I don't need anything too fancy. :wink:

Thanks! This works just great!
Post Reply