Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Getting the base url, how?

    Hi,

    I'm having a script http://www.domain.com/install/install.php

    I want to that install.php to show the website: http://www.domain.com <- without any end slashes or anything crap! The domain.com is a address that change often so I cannot just type my address, I need the php file to check it itself.

    I can not solve this myself wiht $_SERVER["SERVER_ADDR"], is there any else?

    Thanks!
    PHP 5 & MySQL 5 (Y)

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Code:
    $_SERVER['HTTP_HOST']
    Code:
    http://www.mydomain.com/page/to/my/index.php?par=1
    $_SERVER['SERVER_PROTOCOL'].'://'. $_SERVER['HTTP_HOST']. $_SERVER['PHP_SELF'].'?'. $_SERVER['QUERY_STRING'] 
    
    http://www.mydomain.com/page/to/my/index.php?par=1
    $_SERVER['SERVER_PROTOCOL'].'://'. $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI']
    Last edited by Bill Posters; 06-15-2006 at 03:26 PM.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hmm,

    My script located at http://www.domain.com/my script base dir/install/install.php

    And with that script I can only get wheather
    http://www.domain.com
    or
    http://www.domain.com/my script base dir/install/install.php

    but I can not get http://www.domain.com/my script base dir
    PHP 5 & MySQL 5 (Y)

  • #4
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    You only said the domain name changes, so I presume the rest (the sub-directory structure) doesn't.

    The purpose of my post was to demonsrate which server variables you need to form the base of your url. As the rest will be static, you can hard-code them into the string.

    e.g.
    Code:
    'http://'.$_SERVER['HTTP_HOST'].'/install/install.php'
    Last edited by Bill Posters; 06-15-2006 at 03:27 PM.

  • #5
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Hmm, is there any way to change the
    $address = "http://www.domain.com/dgdf/dfg/install/install.php";
    to go to 2 directories back like
    http://www.domain.com/dsgdfg/fdggf ?

    it's like <a href="../../directory"></a> <--- 2 dir back

    Help?
    PHP 5 & MySQL 5 (Y)

  • #6
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Code:
    $localroot = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    $localroot = str_ireplace('/install/install.php','',$localroot);
    echo $localroot;
    What this basically does is construct a string of the entire url:
    www.domain.com/route/to/install/install.php

    Locate the string /install/install.php within that $localroot url string and replace it with nothing, effectively removing it.

    It then echos our the remaining part of the $localroot url string

    e.g.
    www.domain.com/route/to


    (Fwiw, the code could be condensed into a single line*, but I figure you'd appreciate the clarity of having it more open.)

    *
    Code:
    echo str_ireplace('/install/install.php','',$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    Last edited by Bill Posters; 06-15-2006 at 03:57 PM.

  • #7
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Yeah. Now it works! Thanks dude!

    One strange thing,

    It seems to not understand what is str_ireplace, but it worked well with str_replace

    Did you type ireplace correct or why it doesn't work for me? The str_replace worked but not the ireplace one...
    PHP 5 & MySQL 5 (Y)

  • #8
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Quote Originally Posted by kaisellgren
    It seems to not understand what is str_ireplace, but it worked well with str_replace

    Did you type ireplace correct or why it doesn't work for me? The str_replace worked but not the ireplace one...
    Your hosting is probably running PHP4, rather than PHP5.
    str_ireplace (a case insensitive version of str_replace) came in with PHP5, but str_replace will do you fine so long as you stick to using the same casing in your directory and file name and the str_replace function.

    i.e.
    str_replace('/install/install.php',… will work with www.domain.com/route/to/install/install.php, but it won't work with www.domain.com/route/to/Install/Install.php

  • #9
    Regular Coder
    Join Date
    Jan 2006
    Location
    Finland, Hollola
    Posts
    285
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Ah okay. That's good. I'm only using lower case letters in all files and folder names.

    Thanks for giving this good info!
    PHP 5 & MySQL 5 (Y)


  •  

    Posting Permissions

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