what is the difference between $_SERVER['REQUEST_URI'] and $_GET['q']?

Solutions

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.

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.

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

Tags: PHP / Drupal

Similar questions

Drupal Coding Standards (request_uri)
I'm trying to write a patch to make module Drupal coding standards. And this one the complains that have and I do not know how to fix it. Line 148: the use of REQUEST_URI is prone to XSS exploits and does not work on IIS; use request_uri() instead [security_12] return "http" . (isset($_SERVER['HTTPS']) ? "s" : "") . "://" . "{$_SERVER['HTTP_HOST']}...
With REQUEST_URI i get id node or alias path?
I have a big doubt. In template.php I am using: $_SERVER['REQUEST_URI'] what will the output like: node/id-numberor /Alias_page ?
Alter existing 'Log In' route so URL includes request_uri as query string
I am trying to alter the existing 'Log In' URL so that it points to a certain location and also includes the REQUEST_URI as a query string that is not encoded. How do I do this? Based on this article, Altering existing routes and adding new routes based on dynamic ones and this SE answer I can change the 'Log In' URL so it points to a different loc...
What is the difference between "Limit allowed HTML tags and correct faulty HTML" and "Correct faulty and chopped off HTML"?
In CKEditor Filter processes (/admin/config/content/formats/manage/filtered_html) there are filters. If you use "Limit allowed HTML tags and correct faulty HTML" do you still also need the other one, or does this one do all that, plus limit?
Custom exposed filter with single on/off checkbox doesn't respect the default value and the $_GET parameter
I've created a custom filter with a checkbox exposed field in my module. When by default in Views UI settings checkbox is disabled, everything works fine; when it's enabled, it doesn't work. It doesn't respect the default value and the $_GET parameter. I can't understand this strange behavior of single on/off checkboxes in exposed filters in a view...
How to get $_POST and $_GET parameters
I ajaxly call a route and pass data to it, In my controller, I want to get $_POST['var'] , $_POST['var2'], it seems because of using HttpFoundation in drupal 8, $_POST and $_GET are not exists in old method, How I can access $_POST parameters in custom Controller?

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.