# Thread: define doc. root

1. ## 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?

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. <?=root?>

PS. Why not do

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

It's shorter

3. 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.

4. 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. 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; ```

Fatal error: Call to undefined function root() in D:\JREAM\portfolio\index.php on line 20

PHP Code:
``` <?php include root('/bin/something.inc'); ?> ```
this could work for placing it in multiple places:
PHP Code:
``` <?phpDEFINE ("root", \$_SERVER['DOCUMENT_ROOT']);function root(\$place){    echo root . \$place;}?> ```

7. This works, i hope this looks legit:

PHP Code:
``` <?phpfunction root(\$place){    \$server = \$_SERVER['DOCUMENT_ROOT'];    include(\$server . '/' . \$place);}?> ```

8. Originally Posted by JREAM
This works, i hope this looks legit:

PHP Code:
``` <?php function root(\$place){     \$server = \$_SERVER['DOCUMENT_ROOT'];     include(\$server . '/' . \$place); } ?> ```

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

#### Posting Permissions

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