0

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.

6 Answers 6

7

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.";
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1. Now that I re-read the OP's question 3 times, I think this is what he is looking for.
1

Check isset($_GET['var_name'])

http://php.net/isset

1 Comment

... 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.
0

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);

1 Comment

Don't you think that var_dump($_GET) or print_r($_GET) more useful than foreach loop?
0

More general:

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

Comments

0

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";
}

Comments

0

since $_GET returns an array, it might be safer to check its size using sizeof() function

Example:

 if(sizeof($_GET)>0){
   /*you had passed something on your link*/
 }else{
   /*you did not passed anything on your link*/
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.