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 5 of 5
  1. #1
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post

    Creating URL base

    I am trying to create a base URL from which to build all other resource calls. The file this code is in resides in a /includes folder. I first explode the path then reassemble it leaving off the script filename and the '/include' directory.

    So '//nexus/raw/include/test.php' becomes 'http://nexus/raw/


    Is there a more elegant way of doing this?

    PHP Code:
    // constant for the WWW base - used to assemble web paths
    $temp explode("/"$_SERVER['SCRIPT_NAME']);
    $temp2 'http://' $_SERVER['HTTP_HOST'];
    for (
    $x=0$x count($temp)-2$x++) {
        
    $temp2 .= $temp[$x] . '/';
    }
    define ("URL_BASE"$temp2); 
    Of all the things I've lost in my life time, my mind is the one I miss the most!

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    why cant the include file simply have the hard coded paths ?... e.g. in my config file I always have
    PHP Code:
    <?
    DEFINE
    ('FP_ROOT','http://blah.com');
    DEFINE('FP_FILE_ROOT','/home/httpd/vhosts/blah.com');
    ?>
    If I change hosts I then simply need to change the config file
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post
    Yes.... I had thought about using hard coded paths but I would prefer to have dynamic ones so no one would have to edit the configuration files.

    I also thought it was a better style to try to have dynamic pathing? No ?
    Of all the things I've lost in my life time, my mind is the one I miss the most!

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Well, the benefit of hardcoded paths is you only have to change them once, and they will work regardless of any changes you make to code/settings(provided you used them properly to begin with). For your original question, if the file is always going to be in "include" you could do something like this:
    PHP Code:
    $temp 'http://' $_SERVER['HTTP_HOST'] . substr($_SERVER['SCRIPT_NAME'], 0strripos($_SERVER['SCRIPT_NAME'], 'include/')); 

  • #5
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post
    Thank you both for your responses.
    Of all the things I've lost in my life time, my mind is the one I miss the most!


  •  

    Posting Permissions

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