Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Two quick questions about PHP Includes...

Contributor ,
Dec 30, 2009 Dec 30, 2009

As we know...

<?php include ('/events/popup.php'); ?>

...will not work, because PHP believes the root folder to be the actual root of the hard drive the site is stored on (ie, /home/user/html/etc).

However, someone passed along the following suggestion which SEEMS to work just fine :

<?php include $_SERVER["DOCUMENT_ROOT"].('/events/popup.php'); ?>

I'm guessing our server administrator has the document root -- aka, the root folder of this specific website -- specified in a config file somewhere that we are accessing via this command. Is that correct?

That is question #1.

Question #2 has to do with syntax. Is the proper include syntax :

<?php require ('popup.php'); ?>

or

<?php require ("popup.php"); ?>

or

<?php require "popup.php"; ?>

TOPICS
Server side applications
646
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 30, 2009 Dec 30, 2009

To solve your first issue, take out the leading slash.  events/popup.php will work fine for the path.  You are correct in the assumption that a leading slash goes to the root level of the server/account.

This is a tough question.  I am pretty sure all ways are correct.  The only one I am debating about is A.  B&C are definitely ok.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 30, 2009 Dec 30, 2009

Problem with the first answer is that the inludes and requires are going to be called from a master HEADER file, that will be -- itself -- an include.

Titles, keywords, linked CSS/JS files, and PHP REQUIRES will all be located in "Header.php".

"Header.php" will always be located in the same place. But the files CALLING "Header.php" won't. They will be in various folders and subfolders.

Therefore, I suspect "events/popup.php" will mean a different thing for each one of them. In other words, if http://domain.com/pages/file.php is calling /includes/Header.php, and http://domain.com/file.php is also calling /includes/Header.php, then the files that Header.php is calling can't be relative to the document... they've got to be root-relative, right?

That's why a root-relative path is needed. So that whether the file calling the include is in the root, or 4 subfolders down from the root... the call will remain the same for all of them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 30, 2009 Dec 30, 2009

Thread moved to the Dreamweaver Application Development forum, which deals with PHP and other server-side issues.

mjyeager wrote:

<?php include $_SERVER["DOCUMENT_ROOT"].('/events/popup.php'); ?>

I'm guessing our server administrator has the document root -- aka, the root folder of this specific website -- specified in a config file somewhere that we are accessing via this command. Is that correct?

No, it's a completely wild guess. As the name suggests, the information comes from the server. PHP detects it automatically. The only reservation is that $_SERVER['DOCUMENT_ROOT'] is not supported on all servers. You can check whether it's supported either by trying it, or by checking phpinfo(). If it's supported, it will be listed among PHP Variables right at the bottom of the configuration information.

Your use of $_SERVER['DOCUMENT_ROOT'] is slightly wrong. You should use this:

<?php include $_SERVER['DOCUMENT_ROOT'] . '/events/popup.php'; ?>

If $_SERVER['DOCUMENT_ROOT'] doesn't work, the alternative is to change the PHP include_path so that it searches the correct folder. Alternatively, you can simply use a fully qualified path. Changing the include_path is best done by the server admininstrator or in an .htaccess file (if using Apache). A fully qualified path would look similar to this on a Linux machine:

<?php include '/home/username/public_html/events/popup.php'; ?>

On a Windows server, you might have something like this:

<?php include 'C:/inetput/wwwroot/events/popup.php'; ?>

As for your second question, the parentheses around the path name are optional. Single or double quotes are equally valid.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 30, 2009 Dec 30, 2009

You wrote :

Your use of $_SERVER['DOCUMENT_ROOT'] is slightly wrong. You should use this:

<?php include $_SERVER['DOCUMENT_ROOT'] . '/events/popup.php'; ?>

And :
As for your second question, the parentheses around the path name are optional. 
Single or double quotes are equally valid.

Then I'm not seeing how my use of $_SERVER['DOCUMENT_ROOT'] is wrong (as you claim it is). The only difference between my use and yours are the parentheses and use of double quotes, both of which you yourself have pointed out are just as valid as not using them.

What about your use am I not seeing?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 31, 2009 Dec 31, 2009
LATEST

If you're going to use parentheses, they should go around the whole path like this:

<?php include ($_SERVER['DOCUMENT_ROOT'] . '/events/popup.php'); ?>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines