37

what is the difference between $_SERVER['REQUEST_URI'] and $_GET['q'] (which is used in Drupal)?

alex
  • 479,566
  • 201
  • 878
  • 984
user550265
  • 3,171
  • 8
  • 44
  • 55
  • 31
    Um.. they are two entirely different things? I think you will need to add some context – Pekka Jan 19 '11 at 01:12

3 Answers3

150

Given this example url:

http://www.example.com/some-dir/yourpage.php?q=bogus&n=10

$_SERVER['REQUEST_URI'] will give you:

/some-dir/yourpage.php?q=bogus&n=10

Whereas $_GET['q'] will give you:

bogus

In other words, $_SERVER['REQUEST_URI'] will hold the full request path including the querystring. And $_GET['q'] will give you the value of parameter q in the querystring.

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
  • 2
    Outside of the context of Drupal your answer would be correct, however in Drupal 7 `$_GET['q']` stores the actual path (not an alias) after the Drupal base folder. It's function is the same ar `current_path()`, see https://api.drupal.org/api/drupal/includes%21path.inc/function/current_path/7.x It is removed in Drupal 8. – AndrewRMillar Oct 10 '19 at 07:00
18

In the context of Drupal, the difference will depend whether clean URLs are on or not.

With them off, $_SERVER['REQUEST_URI'] will have the full path of the page as called w/ /index.php, while $_GET["q"] will just have what is assigned to q.

With them on, they will be nearly identical w/o other arguments, but $_GET["q"] will be missing the leading /. Take a look towards the end of the default .htaccess to see what is going on. They will also differ if additional arguments are passed into the page, eg when a pager is active.

mpdonadio
  • 2,891
  • 3
  • 35
  • 54
  • 5
    This is the only answer that tackles the initial reason for the question (url rewriting); instead of just giving the rather obvious php superglobals definitions. Don't get me wrong, the other answers are very correct in themselves, but this answer provides some background to the question and really should have more upvotes! – Levite Apr 08 '15 at 20:06
11

The PHP manual explains both quite well:

http://php.net/manual/en/reserved.variables.server.php # REQUEST_URI

http://php.net/manual/en/reserved.variables.get.php # for the $_GET["q"] variable

mario
  • 144,265
  • 20
  • 237
  • 291