In PHP, how can I get the URL of the current page? Preferably just the parts after http://domain.example.
 
    
    - 23,933
- 14
- 88
- 109
 
    
    - 261,656
- 265
- 575
- 769
- 
                    Please see this answer: http://stackoverflow.com/questions/6768793/php-get-the-full-url/8891890#8891890 – Timo Huovinen Oct 19 '13 at 11:27
- 
                    Easy way :) $url = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')?"https://":"http://".$_SERVER['HTTP_HOST']; – Pirooz Jenabi Apr 22 '23 at 12:48
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']
 
    
    - 170,088
- 45
- 397
- 571
 
    
    - 507,862
- 82
- 626
- 550
- 
                    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.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']; ?>
 
    
    - 23,933
- 14
- 88
- 109
 
    
    - 1,271
- 1
- 8
- 4
- 
                    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; }` – 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
- 
                    how about replacing http with REQUEST_SCHEME: $_SERVER['REQUEST_SCHEME']. "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] – aerobrain Jul 20 '23 at 09:21
 $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'];
 
    
    - 60,743
- 20
- 130
- 150
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.
 
    
    - 18,086
- 12
- 58
- 77
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,885
- 2
- 25
- 25
 
    
    - 77
- 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