How to get current page URL in PHP
In this post we will give you information about How to get current page URL in PHP. Hear we will give you detail about How to get current page URL in PHPAnd how to use it also give you demo for it if it is necessary.
Use the PHP $_SERVER
Superglobal Variable
You can use the $_SERVER
built-in variable to get the current page URL in PHP. The $_SERVER
is a superglobal variable, which means it is always available in all scopes.
Also if you want full URL of the page you’ll need to check the scheme name (or protocol), whether it is http
or https
. Let’s take a look at an example to understand how it works:
<?php
$uri = $_SERVER['REQUEST_URI'];
echo $uri; // Outputs: URI
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $url; // Outputs: Full URL
$query = $_SERVER['QUERY_STRING'];
echo $query; // Outputs: Query String
?>
i hope you like this small article.
Hope this code and post will helped you for implement How to get current page URL in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs