In PHP, how can I get the URL of the current page? Preferably just the parts after http://domain.com.
-
Please see this answer: http://stackoverflow.com/questions/6768793/php-get-the-full-url/8891890#8891890 – Timo Huovinen Oct 19 '13 at 11:27
-
14I find it funny that this question is marked as a duplicate of another despite being asked two years earlier – cameronjonesweb Feb 22 '16 at 04:42
-
6@cameronjonesweb And that the other question has a totally different scope (getting the full URL), as opposed to this one (getting the current page only) – person27 Dec 04 '16 at 20:35
5 Answers
$_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']
- 169,393
- 45
- 393
- 567
- 477,764
- 81
- 611
- 541
-
7
-
13iirc, 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
-
1Err, 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
if you want just the parts of url after http://domain.com, try this:
<?php echo $_SERVER['REQUEST_URI']; ?>
if the current url was http://domain.com/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']; ?>
- 1,201
- 1
- 8
- 4
-
18Should 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
-
-
@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
$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'];
- 59,289
- 20
- 126
- 148
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.
- 17,276
- 12
- 55
- 76
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
- 2,427
- 2
- 22
- 24
- 79
- 1
- 1
-
-
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
-
11People 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
-
1Also, 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