tags:

views:

206

answers:

5

I have a webpage to which any amount of URL varibales could be set.. examples:

  • index.php?source=lol
  • index.php?source=lol&sub=haha
  • index.php?aff=123
  • index.php?keyword=pizza

I want a way that I can detect that any url variable has been set, if a url variable has been set I want to print something on the page. Any ideas? I couldn't find anything on Google about this.

+1  A: 

Check isset($_GET['var_name'])

http://php.net/isset

Matt
... in conjunction with $_GET or $_REQUEST, which contain the URL variables. If you want to process them further, be sure to inform yourself about how to handle user input securely.
Pekka
A: 

You can see if a variable has been set using isset or array_key_exists:

if (isset($_GET['source']))
    doSomething();

You can loop over all the querystring variable like this:

foreach ($_GET as $key => $value)
    echo htmlspecialchars($key) . ' is set to ' . htmlspecialchars($value);
Greg
Don't you think that var_dump($_GET) or print_r($_GET) more useful than foreach loop?
Sergey Kuznetsov
+5  A: 

count($_GET); will return the number of parameters in the URL. Use if (count($_GET) > 0) to test for their presence.

For example:

if (count($_GET) > 0){
    print "You supplied values!";
} else {
    print "Please supply some values.";
}
MiffTheFox
+1. Now that I re-read the OP's question 3 times, I *think* this is what he is looking for.
Matt
A: 

More general:

if (count($_GET)) {
 foreach ($_GET as $key => $value) {
  echo "Key $key has been set to $value<br />\n";
 }
}
Augenfeind
A: 

If you want to check if any variables has been sent, use the function below.

function hasGet()
{
    return !empty($_GET);
}

if (hasGet()) {
    echo "something on the page";
}
Znarkus