-1

I am making a forum that accesses threads based off the category in the URL using the GET method. I want to redirect to an error page if no parameters exist in the url, but I want this to be a generic piece of code that can be used around my whole site.

For example:

The url would normally contain the category id:

localhost/myforum/threads.php?categoryid=1

I want it so that when the url is:

localhost/myforum/threads.php

it is to redirect to an error page, and that this piece of code is usable all around the website

rockeh900
  • 27
  • 1
  • 1
  • There is the answer in your question. Check $_GET array, or http://php.net/manual/en/function.parse-url.php. – Peter Apr 24 '17 at 01:34

4 Answers4

7

The most reliable way is to check if the URL contains a question mark:

if (false !== strpos($_SERVER['REQUEST_URI'], '?')) {
    // There is a query string (including cases when it's empty)
}
Marat Tanalin
  • 13,927
  • 1
  • 36
  • 52
2

Try:

$gets = parse_url($url));
if($gets['query'] == "")
{
    echo "No GET variables";
}
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
Ali Rasheed
  • 2,765
  • 2
  • 18
  • 31
1

Just:

if (empty(array_diff($_GET, ['']))) {
    header("Location: /path/to/error.php");
}

EDIT: Updated to remove empty values

0

You can use is_set to check if the parameter exists like this,

isset($_GET)
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 1
    `isset` not `is_set` and this will only check for a parameter called categoryid, not others. –  Apr 24 '17 at 01:54