Getting Current Page URL with PHP

If you would like to dynamically include a Facebook “like” button on your pages, then you will need to know the pages FULL url (rather than the file name of the page). There are, of course, other uses for knowing the full URL to page other than Facebook “Like” buttons – but that was what I personally needed the URL for. ┬áThis PHP Snippet:

function pageurl() {
$prefix = "https://";
$prefix = "http://";
return $pageURL;

will return the URL of the page. The first six lines check to see if the server is communicating with the requesting machine over HTTP or HTTPS, and sets the prefix accordingly.

Line 7 then creates the URL string, combining the prefix, the server name (or the domain name), and the path to the requested files. Line 8 then returns the URL String, which can be echoed like so:

echo pageurl();