Development PHP: Simplify These?

Discussion in 'Software' started by jezmck, 14 Feb 2008.

  1. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    I'm sure it's possible to simplify the following two lines, but my brain is not up to speed this morning, can anyone help?
    Code:
    $root_url = str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(str_replace(basename(__FILE__), '', __FILE__).'/..'));
    $root_dir = $_SERVER['DOCUMENT_ROOT'].$root_url;
     
  2. Cinnander

    Cinnander What's a Dremel?

    Joined:
    19 Apr 2007
    Posts:
    393
    Likes Received:
    2
    It might be but I can't see what you're after... what is it trying to do?
    What is the first line supposed to be doing? Here's what it seems to be doing to me (I expanded this out a bit)
    Code:
    // removes the path part of a filename (/var/www/mypage.com/stuff/page.php -> page.php)
    $w = basename(__FILE__);
    
    /* replace page.php with "/.." in /var/www/mypage.com/stuff/page.php -> /var/www/mypage.com/stuff//..
     * nb could just use .. not /.. ? */
    $x = str_replace($w, '', __FILE__).'/..'
    
    // cleans it up to an actal path (/var/www/mypage.com/stuff//.. -> /var/www/mypage.com/stuff/.. -> /var/www/mypage.com/)
    $y = realpath($x);
    
    /* remove the document root (/var/www/www.mypage.com) -> empty string if document root is the same
     * does nothing if they are different */
    $root_url = str_replace($_SERVER['DOCUMENT_ROOT'], '', $y);
    So you'll either have '' or something like /var/www/www.mypage.com/ if the server document root and the file path differ
     
  3. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    Why I need these lines:
    I include this code (in a file) in various files in various locations.
    I need $root_url for relative URLs, and $root_dir for file include paths.
    The system I'm building may be deployed to various servers and hence in various folders.

    It does indeed appear that they're flawed.
     
  4. Firehed

    Firehed Why not? I own a domain to match.

    Joined:
    15 Feb 2004
    Posts:
    12,574
    Likes Received:
    16
    My approach:
    PHP:
    //in files that need this data:
    include 'static.php';
    //in static.php
    define('_BASE_SITE_URL','http://localhost/mct/');
    :)

    Maybe not quite as infinitely flexible, but a whole heck of a lot easier and I expect easier on resources as well. Make a couple defines in static.php for different _BASE_SITE_URL and comment out all but one if you've got a local and a remote version of the site.
     
  5. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    The problem is that the system is updated to various locations via CVS.
    I just figure there must be someway of writing the code I need, and it's eluding me...
     
  6. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    Can you not make use of $_SERVER['HTTP_HOST'] ?
     
  7. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    of course, but it doesn't help me with the variable locations issue.

    a different version I've just dug up:
    Code:
    define('FILE_ROOT', realpath('.').'/');
    $DIR_PHP_SELF = dirname($_SERVER['PHP_SELF']);
    $SITE_ROOT = $DIR_PHP_SELF == '/' || $DIR_PHP_SELF == '\\' ? '' : dirname($_SERVER['PHP_SELF']);
    define('SITE_ROOT', $SITE_ROOT);
    I'm surprised that I've never found a pre-written solution, it seems to me like a common requirement.
     
  8. Firehed

    Firehed Why not? I own a domain to match.

    Joined:
    15 Feb 2004
    Posts:
    12,574
    Likes Received:
    16
    you could always explode() the $_SERVER['HTTP_HOST'] by a slash, count the number of elements in the resulting array, and pull the appropriate one(s) into a new variable.
     

Share This Page