Friday, March 2, 2018

How to get current page URL using PHP

In this tutorial, we are going to discuss how to get current page URL using PHP. $_SERVER is a super global variable, which is always available in all scopes. We will use $_SERVER variable to get the current page URL. It is an array contains various information such as, headers, document path, script location, request method etc.

Explanation about some useful $_SERVER variable :

$_SERVER['HTTP_HOST'] : Contents of the Host header from the current request, if there is one.

$_SERVER['PHP_SELF'] : The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://skptricks.com/post/php-tutorial.php would be /post/php-tutorial.php.

$_SERVER['QUERY_STRING'] : If a page is accessed via any query string, $_SERVER['QUERY_STRING'] fetches that query string.This is a actual URL http://localhost/skptricks/check current page url/currentPageURL.php?jj=kkkk after the $_SERVER['QUERY_STRING'] operation would be jj=kkkk .

Get Current Page URL in PHP :

Lets see the following simple PHP function with help of that you can get the current page URL with or without the query string as per your need.

Output: Current page URL without query string : http://localhost/skptricks/check current page url/currentPageURL.phpCurrent page URL with query string : http://localhost/skptricks/check current page url/currentPageURL.php?jj=kkkk
This is all about fetching current page URL using PHP. Hope you like this simple example. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.Download Link :https://github.com/skptricks/php-Tutorials/tree/master/How%20to%20get%20current%20page%20URL%20using%20PHP