Pick a category to read specific topics. Feel free to comment on any post.
Jul 27th

PHP: Getting the full URL of the current page

A URL like http://www.somedomain.com/index.php?act=view contain three parts:

Part 1 – Domain name – www.somedomain.com
Part 2 – Path of current file – index.php
Part 3 – Query string – act=view

In PHP, it isn’t possible to get all three parts by using one method. We need to get each of the parts seperately then put it all together.

The code example below shows you how to retrieve each of these parts and then putting it all together for the full URL of the current page.

// find then domain:
$domain = $_SERVER['HTTP_HOST'];
// find the path to the current file:
$path = $_SERVER['SCRIPT_NAME'];
// find the Query String:
$queryString = $_SERVER['QUERY_STRING'];
// putting it all together:
$url = "http://" . $domain . $path."?" . $queryString;;

It is extremely simple isn’t it?

Leave a comment