193

In PHP, how can I get the URL of the current page? Preferably just the parts after http://domain.example.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Ali
  • 261,656
  • 265
  • 575
  • 769

5 Answers5

339
$_SERVER['REQUEST_URI']

For more details on what info is available in the $_SERVER array, see the PHP manual page for it.

If you also need the query string (the bit after the ? in a URL), that part is in this variable:

$_SERVER['QUERY_STRING']
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Amber
  • 507,862
  • 82
  • 626
  • 550
  • 7
    You can also use $_SERVER['PHP_SELF'] – Alix Axel Aug 16 '09 at 02:12
  • 13
    iirc, PHP_SELF and REQUEST_URI will have different values if the page was redirected via mod_rewrite - the former has the path to the actual script, the latter has the originally requested path. – Amber Aug 16 '09 at 02:19
  • 1
    Err, at least in my apache, 2.2.4, with php 5.3, REQUEST_URI contains the stuff after the ? already... – Kzqai Aug 03 '11 at 16:57
  • 2
    `$_SERVER['REQUEST_URI']` is also contaning all query strings, why should I use `$_SERVER['QUERY_STRING']` ? – Shafizadeh Sep 14 '15 at 22:40
117

If you want just the parts of URL after http://domain.example, try this:

<?php echo $_SERVER['REQUEST_URI']; ?>

If the current URL was http://domain.example/some-slug/some-id, echo will return only /some-slug/some-id.


If you want the full URL, try this:

<?php echo 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Jonas Orrico
  • 1,271
  • 1
  • 8
  • 4
  • 19
    Should you not check if `HTTPS://` is enabled? I found this function to check: `function isSSL() { return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443; }` – Dendromaniac May 27 '15 at 15:39
  • if you just want the domain. – Erik Thiart Jul 24 '18 at 07:14
  • @ErikThiart, I tried and it's displaying like example.com I need like http://www.example.com. Is it possible? – user9437856 Jan 29 '21 at 11:51
  • how about replacing http with REQUEST_SCHEME: $_SERVER['REQUEST_SCHEME']. "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] – aerobrain Jul 20 '23 at 09:21
29
 $uri = $_SERVER['REQUEST_URI'];

This will give you the requested directory and file name. If you use mod_rewrite, this is extremely useful because it tells you what page the user was looking at.

If you need the actual file name, you might want to try either $_SERVER['PHP_SELF'], the magic constant __FILE__, or $_SERVER['SCRIPT_FILENAME']. The latter 2 give you the complete path (from the root of the server), rather than just the root of your website. They are useful for includes and such.

$_SERVER['PHP_SELF'] gives you the file name relative to the root of the website.

 $relative_path = $_SERVER['PHP_SELF'];
 $complete_path = __FILE__;
 $complete_path = $_SERVER['SCRIPT_FILENAME'];
Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
14

The other answers are correct. However, a quick note: if you're looking to grab the stuff after the ? in a URI, you should use the $_GET[] array.

Imagist
  • 18,086
  • 12
  • 58
  • 77
-2

You can use $_SERVER['HTTP_REFERER'] this will give you whole URL for example:

suppose you want to get url of site name www.example.com then $_SERVER['HTTP_REFERER'] will give you https://www.example.com

Nik
  • 2,885
  • 2
  • 25
  • 25
user2862106
  • 77
  • 1
  • 1
  • Why does my $_SERVER variable doesn't contain this item?? – Frankie Drake Sep 19 '14 at 10:47
  • 1
    `$_SERVER['HTTP_REFERER']` does exactly what it says on the tin, and that is get the URL of the page that sent the user to the page... I.E The referer. – Dendromaniac May 27 '15 at 15:41
  • 11
    People who upvoted this answer will have some trouble debugging their code. `HTTP_REFERER` is not the current page, it's the page user was on prior to the current page. – AliBZ May 05 '16 at 22:18
  • 1
    Also, this isn't always reliable. It is set by the user_agent, not the server -- so, as the PHP manual says (http://php.net/manual/en/reserved.variables.server.php), "In short, it cannot really be trusted." – kittykittybangbang Sep 05 '17 at 14:17