SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    define doc. root

    Before I go put this in every page (well the define part is an include) i wanted to see if anyone knows of a short way?

    head.inc
    PHP Code:
    <?php

    $root 
    $_SERVER['DOCUMENT_ROOT'];
    DEFINE ('root'$root);

    ?>
    page.xyz
    PHP Code:
    <?php echo root?>
    Is there a way to do: <?php root; ?> without the echo?
    What do you all use?

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,082
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    <?=root?>

    PS. Why not do

    PHP Code:
    define('root'$_SERVER['DOCUMENT_ROOT']); 
    ?

    It's shorter

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Don't define it on every page.

    Put it in a file called constants.inc.php or something and include() it in the other files.

    That way you can add more constants, change them, whatever, easily.

    P.S. Using <?=$var?> is unrecommended - use long tags.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what does <?= do?
    I dont use short-tags ever (compatibility i guess i like to be flexible), is this equivalent:
    <?php=root;?>

    I am going to define it in head.inc file -- which is included on every page -- then i am going to use the constant, would be a big mess to mix that up!

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There is no long tag version, no.

    But it's not that much different to use <?php echo root; ?>

    Btw, it's better to define constants in capitals (for readability) i.e:
    PHP Code:
    <?php
    define
    ('ROOT'$_SERVER['DOCUMENT_ROOT']);
    echo 
    ROOT;
    ?>
    But if you simply make it a variable, you can then use it in strings:
    PHP Code:
    <?php
    $root 
    $_SERVER['DOCUMENT_ROOT'];
    //further on...
    echo <<<CONTENT
       <div id="somediv">
           <p>
    {$root}</p>
       </div>
    CONTENT;
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In not too sure about this:
    Fatal error: Call to undefined function root() in D:\JREAM\portfolio\index.php on line 20

    what would be a way to go about this?
    PHP Code:
    <?php include root('/bin/something.inc'); ?>
    this could work for placing it in multiple places:
    PHP Code:

    <?php

    DEFINE 
    ("root"$_SERVER['DOCUMENT_ROOT']);

    function 
    root($place){
        echo 
    root $place;
    }
    ?>

  7. #7
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works, i hope this looks legit:

    PHP Code:
    <?php

    function root($place){
        
    $server $_SERVER['DOCUMENT_ROOT'];
        include(
    $server '/' $place);
    }
    ?>

    <?php root('header.php'); ?>

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,082
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by JREAM View Post
    This works, i hope this looks legit:

    PHP Code:
    <?php

    function root($place){
        
    $server $_SERVER['DOCUMENT_ROOT'];
        include(
    $server '/' $place);
    }
    ?>

    <?php root('header.php'); ?>
    This works, but be sure to check $place

    For example, if $server = '/var/www/mysite/' and somehow get it working that $place = '../../../etc/passwd' , you would include /etc/passwd file in your website. I guess you don't want that


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
  •