Forum Moderators: coopster

Message Too Old, No Replies

Question about PHP Includes

         

Sub_Seven

6:53 pm on Sep 12, 2010 (gmt 0)

10+ Year Member



Hey people,

Hopefully this is a quick one, I am building a website with a lot of PHP includes, one of the goals is to have one single include to handle the header all across the website, let's say this is the header file:


<a href="index.php"><img src="images/logo.png" id="logo" /></a>
<header>
<nav>
<ul>
<li><a href="index.php">Home page</a></li>
<li><a href="about-us.php">About us?</a></li>
<li><a href="brands/index.php">Brands</a></li>
</ul>
</nav>
</header>


Any pages under the same directory (root) will work fine, the logo image is displayed and the links are functional.

The problem is when I include the header.php include in a page that belongs to a subdirectory, let's say for example a page located at: "brands/index.php"

I change the include on those pages so that it can be seen by adding the "../" (or more according to directory depth) but the image files and links are not functional.

Does this mean I need to have on different php include of the same file specific to each directory level to make this work?

I hope there is a way to keep one single file, If anyone can give me a hint it would be much appreciated?

Thanks :)

optik

6:58 pm on Sep 12, 2010 (gmt 0)

10+ Year Member



Just start any HTML file reference with a forward slash e.g href="/brands/index.php", that will use the web root directory as the base of the file structure. This means you always need to write the link with the full folder structure from the web root to the file.

Matthew1980

7:03 pm on Sep 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Sub_Seven,

Something I thought I should mention:-

<a href="index.php"><img src="images/logo.png" id="logo" /></a>
<header>
<nav>
<ul>
<li><a href="index.php">Home page</a></li>
<li><a href="about-us.php">About us?</a></li>
<li><a href="brands/index.php">Brands</a></li>
</ul>
</nav>
</header>


Those html tags don't exist so far as I am aware? Please correct me if I am wrong.

Cheers,
MRb

optik

7:35 pm on Sep 12, 2010 (gmt 0)

10+ Year Member



I think header is in HTML 5 but yeah, not a good idea for IE.

Matthew1980

8:00 pm on Sep 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Optik,

I have just checked out the new structural tags for HTML 5 and <header> & <footer> are correct.

I stand corrected!

Cheers,
MRb

Sub_Seven

1:53 am on Sep 13, 2010 (gmt 0)

10+ Year Member



Thanks optik, that works great.

Yes Matthew, I'm pretty excited about using HTML5, as far as compatibility with IE there is a repo here [code.google.com...] to fix the issue. All other main browsers work fine with most of the HTML5 elements I've used so far (at least their latest versions...)

Thanks for the help guys :)

rocknbil

2:56 pm on Sep 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just for clarification,

Just start any HTML file reference with a forward slash e.g href="/brands/index.php",


This is correct for links and other file references from the browser but since the topic is about includes, it would be incorrect to include a PHP file this way. You would need a file system path, not a url.

include($_SERVER['DOCUMENT_ROOT']."/directory/some-include.php");

You may need to right trim the document root if it has a slash (some systems do.)

g1smd

9:19 pm on Sep 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't link to named index files in clickable links.

Replace links to "/folder/index.php" with links to "/folder/" etc.

The root is "/" or "http://www.example.com/".

Sub_Seven

2:46 am on Sep 14, 2010 (gmt 0)

10+ Year Member



Thanks for the extra tips guys,

@rocknbil:

So far things are working fine, I'm just starting this website and I've only gone through one level over the root. I'd still like to know the best way to do things; are you saying that this is wrong:

<?php include '../includes/header.php'; ?>

and that I should replace "../" for "$_SERVER['DOCUMENT_ROOT']." when using php includes?

@g1smd:

Thanks, that one I knew, but it does raise a question, maybe you have an answer...

To avoid duplicate content one should 301 either or, is "/" better than "/index.php" or vice versa or the difference is purely esthetic and I should bother which to pick?

Thanks :)

astupidname

11:14 am on Sep 14, 2010 (gmt 0)

10+ Year Member



are you saying that this is wrong:

<?php include '../includes/header.php'; ?>

and that I should replace "../" for "$_SERVER['DOCUMENT_ROOT']." when using php includes?


That is the gist of what rocknbil is getting at, I believe. I usually avoid using relative paths for includes, instead I usually define paths for includes (use the same definition at top of every file) as well as site root:

if (!defined('ROOT')) {
define('ROOT', preg_replace('/\/$/', '', $_SERVER['DOCUMENT_ROOT'])); //the root of the site
define('PHPFOLDER', preg_replace('/[^\/]+$/', 'php', ROOT)); //above root 'php' folder
define('MYPHPFOLDER', PHPFOLDER.'/my_php'); //above root 'php/my_php' folder
}

include(MYPHPFOLDER.'/header.php');


There are usually many php files you will not want publicly available on or above the site's root, so usually you would store them above the sites root if they are just includes or functional code/class definitions etc. Exceptions to that would be files meant as accessible for ajax, or direct views, which of course need to be on or below site root.

is "/" better than "/index.php" or vice versa or the difference is purely esthetic and I should bother which to pick?


"/" is preferred. If you use that as a links href it means h ttp://www.example.com/ whereas if you use "/index.php" it means h ttp://www.example.com/index.php . People don't want to remember "index.php" or to add the file extension on, so in the case of a file about widgets, will users more likely remember h ttp://www.example.com/widgets/ or h ttp://www.example.com/widgets.php or h ttp://www.example.com/widgets/widgets.php or h ttp://www.example.com/widgets/index.php ? Hmmm.. actually most people don't remember h ttp://www.

rocknbil

4:13 pm on Sep 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<facepalm> Of course, if it's an indexed file don't name it, use /. :-P You should also add a 301 rewrite for any requests for /index.php (etc.) to /. Besides the reasons above, logically your domain is example.com, not example.com/index.php - you're correct in that this is done more to avoid duplicate content than aesthetics.

are you saying that this is wrong:

<?php include '../includes/header.php'; ?>


It's not wrong if it works, it just becomes hard to work with and maintain, see previous post. I just wrapped up a huge revision of a site, all the includes were dumped in root, there were directories using this syntax, I had to track them down and organize it in some sensible fashion. If you use the document root variable, which is always the full path to the include, you can move a file anywhere.

same is true of / in URL's, you can move the files anywhere. Another aspect of the project I just finished, client wanted to move the site from example.com to example2.com, get everything working on example2.com before switching the DNS. But in various locations there were links to example2.com/file . . . . . changing all those to just /file made the move very easy.

And of course . . . changing instances of /index.php to / :-)

Exception: When you have a secure section of a site, you will need full URL's in links to and from the secure sections.

<a href="h ttp://www.example.com/">Home</a>
<a href="https://www.example.com/checkout/">Check Out</a>

The full path and leading / in URL's is helpful here too - if it's on https, a URL to /images/file.jpg will still be over https.

Sub_Seven

12:42 am on Sep 16, 2010 (gmt 0)

10+ Year Member



Wow, this is great, way more information than I was expecting, awesome, thanks for all the tips, I will definitely be putting all these things in production ASAP,

Thank you all for the help :)