Wednesday, June 29, 2016

How To Get Current URL Without Page Name In PHP

Today I will share a code snippet which will help you get a current URL Without Page Name.

<?PHP

function getPath() {
$protocol = strpos(strtolower($_SERVER[SERVER_PROTOCOL]),https) === FALSE ? http : https;
$host = $_SERVER[HTTP_HOST];
$path = substr($_SERVER[SCRIPT_NAME], 0, -strlen(basename($_SERVER[SCRIPT_NAME])));

return $protocol.://.$host.$path;
}

echo getPath();

?>

This simple piece of code will do the trick.

If you want more such tips and tricks related to PHP then consider subscribing to our blog.

Enjoy!