PHP Magic Book – Free PHP Scripts, Tutorials and Downloads, PHP AtoZ Reloaded, free php tutorials, free php downloads, php scripts, PHP tips

Find Page URL With PHP – File Path

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
http://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

  • Share/Bookmark
Tags: , ,
Posted in: URL, Useful Scripts
Post's RSS » RSS 2.0
Post's Comments RSS » RSS 2.0

Related Posts




Post a Comment

  Subscribe Via RSS
  Subscribe Via Email