Help - Search - Members - Calendar
Full Version: ? How to Include file + same behavior in different folder levels
HTMLHelp Forums > Programming > Server-side Scripting
CorreoHS
Help needed.
I have 3 domains pointing same directory in my webserver (ISP)
mysite.com
mysite.net
mysite.info

I wanted to use (1) one menu file for all of them, that I load it to the include subfolder.

mywebsite structure

/index.php ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/about.php ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/contactus.php ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/(more files) ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/link/
/link/index.php ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/link/(more php files) ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/include
/include/menu.html

Menu File Structure
<li><a href="aboutus.php">ABOUT</a>
<ul><li><a href="aboutus.php">ABOUT US</a></li>
<li><a href="contactus.php">CONTACT US</a></li>
<li><a href="testimonial.html">GUEST BOOK</a></li>

</ul>
</li>

from my php application I use
include $_SERVER['DOCUMENT_ROOT']."/include/menu.html";

Pages on root directory are working well.
/aboutus.php
/contactus.php
/testimonial.html

when I'm on /link directory (subfolder )I got problems
the menu will point to.
/link/aboutus.php
/link/contactus.php
/link/testimonial.html

Getting page no found.

if I'm in a subfolder, how can I do to call the include file, and preserver the root path automatically.

---- let me try to make simpler ----
3 mysites domain
mysite.com mysite.net mysite.info
.com .net .info

/
/index.php
/about.php
/contactus.php
/testimonial.html
/(more files)
/link/
/link/index.php
/link/(more php files)
/include/menu.html

How can I call /include/menu.html from any directory level and maintain same behavior?
------

Thanks!
CorreoHS
Brian Chandler
QUOTE(CorreoHS @ Sep 28 2009, 12:04 PM) *

Help needed.
I have 3 domains pointing same directory in my webserver (ISP)
mysite.com
mysite.net
mysite.info

I wanted to use (1) one menu file for all of them, that I load it to the include subfolder.

mywebsite structure

/index.php ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/about.php ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/contactus.php ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/(more files) ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/link/
/link/index.php ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )
/link/(more php files) ( I use include $_SERVER['DOCUMENT_ROOT']."/include/menu.html"; )



This is Bad Programming. Look, the whole point of using a computer (outside the world of Micro$heep Push-n-Shove ™) is that your program saves you repetitive tasks. So you write a function

function includemyfile($file)
{ include $_SERVER['DOCUMENT_ROOT'] . '/include/' . $file;
}

Then includemyfile('menu.html'); does all the things above.

But anyway, yes, you have made sure you load a file specified from the server root....

QUOTE


Menu File Structure
<li><a href="aboutus.php">ABOUT</a>
<ul><li><a href="aboutus.php">ABOUT US</a></li>
<li><a href="contactus.php">CONTACT US</a></li>
<li><a href="testimonial.html">GUEST BOOK</a></li>

</ul>
</li>



But this is No Good, because it assumes the links are to files in the same directory. Either use links relative to the server root ( href="/aboutus.php" perhaps); or write another function

function linkaddress($filename, $whereweare)
{ ... <work it out yourself> ...
return $address
}

Then you can define a constant in each file WHEREWEARE = '/' or '/links/' or whatever and call

echo linkaddress('aboutus.php', WHEREWEARE);

(for example) to access aboutus.php from whereeverweareatthemoment.

HTH
CorreoHS
Done deal.
As you mentioned, I used links relative to the server root ( href="/aboutus.php")

and I avoided to repeat.
$_SERVER['DOCUMENT_ROOT']

Thanks for your suggestions.

CorreoHS
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.