PHP to get URL of my script

I want to get URL of my script as i will create a config file with my installer and save that url in databse for some reason. But i cant get exact URL of my script. I tried $_SERVER[‘SCRIPTNAME’], $_SERVER[‘SCRIPT_FILENAME’], $_SERVER[‘HOST’]. I dont know how cms like joomla and wordpress get it accurately whether m on localhost with sub directories in it or either on subdomains on live hosting.

Have you tried the “magic constant” FILE ?

I want to get web url like :
not /home/var/wwww/myscript
I am seeking for URL


FILE is the location of the current file in the server’s file structure. It has nothing to do with the requested page.

$_SERVER[‘PHP_SELF’] will be the file that kicked off the process - index.php for Joomla or Drupal.

$_SERVER[‘REQUEST_URI’] is the path the browser is looking for, regardless of what file actually was started by Apache due to the rewrite rules.

From the sound of your request you are looking for FILE which will give you the file path to your script from your script. Just keep in mind that is not the URI the world sees for your script, that’s contained in $_SERVER[‘REQUEST_URI’]. However, the latter is of little use when loading files on the file system.

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";

 return $pageURL;

// use
echo curPageURL();


Also beware that FILE if used in an included file will return the included file’s filename, not the file that is doing the including. (which is what Michael’s first sentence says, in less cryptic terms ;P)

Was looking for something similiar and this is the script I use now to get the current URL

function selfURL(){
list($prot)	= explode('/',strtolower($_SERVER['SERVER_PROTOCOL']));
$s		= $_SERVER['HTTPS'] == 'on' ? 's' : '';
$port		= $_SERVER['SERVER_PORT'] == '80' ? '' : ':'.$_SERVER['SERVER_PORT'];
return $prot.$s.'://'.$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
echo selfURL();

taken from here

Whenever I got confused about this issue I looked at the output of: