Find Page URL With PHP – File Path

by Hiroshi on August 6, 2008

in URL, Useful Scripts

I have three solutions here to find current page name or URL of page being displayed. This script can be useful while you want to get page name or send page URL via email. If the page name is url.php and it is in wamp www directory i.e. localhost, then consider this example.

<?php
$URL1 = $_SERVER['PHP_SELF'];
print $URL1;
?>

Display
/url.php

<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
  echo curPageURL();
?>

Display
https://localhost/url.php

<?php
function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
echo "The current page name is ".curPageName();
?>

Display
The current page name is url.php

Download Script

Related Posts

Previous post:

Next post: