How to Get the Current Page URL of a Web Page Using PHP

PHP



In this tutorial, we show how to get the current page URL of a web page using PHP.


Full URL of Current Page

To get the full URL of the current page, which takes the form http://www.website.com/Directory/pagename.html, you can use the following code below.



The $_SERVER['HTTP_HOST'] gets the www.website.com name of the website in use. For this web page you are current on, this would return www.learningaboutelectronics.com

The $_SERVER['REQUEST_URI'] returns the directory pathway and file name of the page you are on.

Actual PHP Output of this Page


http://www.learningaboutelectronics.com/Articles/How-to-get-the-current-page-url-of-a-web-page-using-PHP.php



Directory Pathway and File name

To return just the directory pathway and the file name of the web page, you can use the following code below.

This is the part after http://www.website.com/



So this code above returns the part after the type of domain name the web page is.

Actual PHP Output of this Page


/Articles/How-to-get-the-current-page-url-of-a-web-page-using-PHP.php



Just the Directory Pathway

To return just the directory pathway of the current web page URL, you can use the following code below.



This code above returns just the directory pathway to the current page URL.

Actual PHP of this Page


/Articles



Just the File Name

To just return the file name of the current page, you can use the following code below.



This code above returns only the file name of the current page URL.

Actual PHP Output of this Page


How-to-get-the-current-page-url-of-a-web-page-using-PHP.php



Just the File Name Without the File Extension

Sometimes you may just want to return the file name without the file extension.

To do this, you can use the following code below.



The code above returns just the file name without the file extension included.

Actual PHP Output of this Page


How-to-get-the-current-page-url-of-a-web-page-using-PHP


Related Resources

How to Create a Global Variable in PHP

Difference between the POST and GET Method in PHP

How to Create a Line Break in PHP

How to Format and Display the Date in PHP

How to Generate a Random Number in PHP

How to Find and Replace Text in PHP

How to Remove Whitespace from a String in PHP

How to Capitalize the First Word of a String in PHP

How to Capitalize Each Word of a String in PHP

PHP print_r() function

PHP foreach loop

How to Check if a Variable is Empty In PHP

How to Measure the Length of a String in PHP



HTML Comment Box is loading comments...