Horje
How to get complete current page URL in PHP ?

In PHP. there is a superglobal variable that can provide you with the URL of the current working page. The $_SERVER is an inbuilt variable that can access the page URL, after that, you have to check that the URL is following which type of HTTP protocol.

What is Superglobal?

Superglobals are already defined variables by the PHP engine which can be used in any kind of scope. They are readily available at any one time.

We understand that by taking an example as given below:

Example 1: We can take the ternary operator for checking the working protocol and after that, we can add server host and request URL. 

PHP

<?php 
  
$url =  isset($_SERVER['HTTPS']) &&
    $_SERVER['HTTPS'] === 'on' ? "https://" : "http://";  
 
$url .= $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];    
 
print_r($url);
 
?>

Output:

Example 2: First we check the HTTP protocol then append that domain to the URL with the request URL. Finally, we get the current page URL.

PHP

<?php 
     
   $currentPageUrl = 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
echo "Current page URL " . $currentPageUrl;
 
  ?>

Output:

 




Reffered: https://www.geeksforgeeks.org


PHP

Related
PHP JSON Pretty Print PHP JSON Pretty Print
How to include content of a PHP file into another PHP file ? How to include content of a PHP file into another PHP file ?
How to get information sent via post method in PHP ? How to get information sent via post method in PHP ?
How to embed PHP code in an HTML page ? How to embed PHP code in an HTML page ?
How to install PHP in windows 10 ? How to install PHP in windows 10 ?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
10