Mike Moore
Mike Moore

Reputation: 7468

Doesn't seem to work: if($_SERVER['REQUEST_METHOD'] == 'GET') for GET requests?

The following code executes whether there are GET variables passed or not:

if($_SERVER['REQUEST_METHOD'] == 'GET')
{
    //Do something
}

The following only executes when GET variables are passed:

if($_GET)
{
    //Do something
}

I was under the impression that the first method was better, but now I am confused.

Any ideas? Thanks!

Upvotes: 1

Views: 5289

Answers (1)

Macmade
Macmade

Reputation: 53960

The first code will execute when the request method is GET, even if no query string is present.
It won't be executed with a POST request type, even if there is a query string.

You have to understand that the 'GET' request type does not mean that variables were passed in the URL.

So the two codes are made for completely different tasks.

If you simply need to check if variables were passed in the URL, use the second one.

Upvotes: 4

Related Questions