In PHP, how can I get the URL of the current page? Preferably just the parts after http://domain.example
.
-
Please see this answer: stackoverflow.com/questions/6768793/php-get-the-full-url/…– Timo HuovinenOct 19, 2013 at 11:27
-
14I find it funny that this question is marked as a duplicate of another despite being asked two years earlier– cameronjoneswebFeb 22, 2016 at 4: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)– person27Dec 4, 2016 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']
-
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.– AmberAug 16, 2009 at 2:19
-
1Err, at least in my apache, 2.2.4, with php 5.3, REQUEST_URI contains the stuff after the ? already...– KzqaiAug 3, 2011 at 16:57
-
2
$_SERVER['REQUEST_URI']
is also contaning all query strings, why should I use$_SERVER['QUERY_STRING']
? Sep 14, 2015 at 22:40
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']; ?>
-
19Should 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; }
May 27, 2015 at 15:39 -
<?php echo $_SERVER['SERVER_NAME']; ?> if you just want the domain. Jul 24, 2018 at 7:14
-
@ErikThiart, I tried <?php echo $_SERVER['SERVER_NAME']; ?> and it's displaying like example.com I need like example.com. Is it possible? Jan 29, 2021 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'];
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.
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
-
-
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. May 27, 2015 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.– AliBZMay 5, 2016 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 (php.net/manual/en/reserved.variables.server.php), "In short, it cannot really be trusted." Sep 5, 2017 at 14:17