How to get URL of current page in PHP

php

Question

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

1
137
8/16/2009 2:06:24 AM

Accepted Answer

$_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']
255
3/9/2018 1:22:18 AM

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']; ?>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon