TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

How to get current page URL in PHP?

Learn simple PHP program; it will help you find current page URL. You can get the full URL or only name of the current page.

<< Back to HOW

How to get current page URL in PHP?

To get the current page URL, PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.

If we want the full URL of the page, then we'll need to check the protocol (or scheme name), whether it is https or http. See the example below:

<?php
	if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') 
   		 $url = "https://"; 
	else
   		 $url = "http://"; 
	// Append the host(domain name, ip) to the URL. 
	$url.= $_SERVER['HTTP_HOST']; 
  
	// Append the requested resource location to the URL 
	$url.= $_SERVER['REQUEST_URI'];  
	
	echo $url;
  ?>	

Output

How to get current page URL in PHP

Note: The isset() function is used here to check whether HTTPS is enabled or not. It checks whether a variable exists or not.

Or, we can also get the full URL of current page using another way given in the next example.

<?php
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$CurPageURL = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo "The URL of current page: ".$CurPageURL;
?>	

Output

How to get current page URL in PHP 1

To get only name of the current page opened at browser, see the below example:

<?php
	$curPageName = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
	echo "The current page name is: ".$curPageName;
	echo "</br>";
  ?>	

Output

How to get current page URL in PHP 1





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf