SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,191
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    Have you tried the "magic constant" __FILE__ ?
    http://php.net/manual/en/language.co...predefined.php

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to get web url like : http://myhost.com/myscript
    not /home/var/wwww/myscript
    I am seeking for URL

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Frankfurt/Germany
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try $_SERVER['PHP_SELF']

  5. #5
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,034
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    __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.

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function curPageURL() {
     
    $pageURL 'http';
     if (
    $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     
    $pageURL .= "://";
     
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

     return 
    $pageURL;
    }

    // use
    echo curPageURL(); 
    ~source

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    __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.
    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)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  8. #8
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Was looking for something similiar and this is the script I use now to get the current URL

    PHP Code:
    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 http://asdlog.com/Get_URL_of_current_page

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Whenever I got confused about this issue I looked at the output of:
    PHP Code:
    print_r($_SERVER); 


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •