Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP includes with sub folders

Featured Replies

Hey.

 

Ive been having trouble with some php includes, basically ive got:

 

header.php

footer.php

index.php

products/page.php

products/page2.php

products/page3.php

 

etc.

 

Now, in my index.php i can just call the header.php with <?php include ('header.php'); ?> (with some variables like $title included).

 

But when i copy that code to say page2.php it doesnt work. Im not sure how to get around this.

 

Any help would be greatly appreciated

 

Cheers.

thats because page2 will be looking for header.php within the products folder. use the following:

<?php include ('/../header.php'); ?>

 

the /.. mens it will go down one level in the hierarchy.

  • Author

Thanks for the reply.

 

I added your code to page2, but i get:

 

Warning: include(/../header.php) [function.include]: failed to open stream: No such file or directory in C:\xampp\htdocs\Website\about\index.php on line 1

 

Warning: include() [function.include]: Failed opening '/../header.php' for inclusion (include_path='.;C:\xampp\php\pear\') in C:\xampp\htdocs\Website\about\index.php on line 1

 

Anyway, surely i want to go up a directory from page2 to find the header.php in the root? (But then again, i have no experience with php, so that could be jibberish!)

take the first / out then. Im not sure whats wrong if that dosn't work :S

  • Author

Hey thanks for the help but its still not working.

 

I dont get any errors but my CSS doesnt load (im assuming becuase in the header.php the css is just "css.css" - ie in the root folder, and its looking for it in the sub folder). Also, none of the links in the nav bar in the header work, they all try to go to the sub folder, eg products/index.php, not just index.php

 

Argh file paths?! What can i do... i swear it shouldnt be this hard.

Hello, I remember running into this problem years ago. As far as I can remember the only way I could solve it was by using an absolute link

  • Author

Thanks for the help.

 

Well, it gets even trickier than what i led on before.

 

You see, when my header.php contains the link to my style.css So even if i manage to call the header.php (which i have) it doesnt load the style.css as it looks for it in the products/style.css

 

?!

I think a good way to solve this is to define a variable at the top of index.php and the product pages before you call the header.

 

For the index.php it would be

 

define("INSUBFOLDER", false);

 

and at the top of the product pages it would be

 

define("INSUBFOLDER", true);

 

then in header.php you can have:

 

$csspath = "style.css";

if($INSUBFOLDER)
 $csspath = "../style.css";

echo "<link rel='stylesheet' type='text/css' href='".$csspath."'>

 

If you see what I mean?

 

I haven't tested this but it should work.

http://us2.php.net/manual/en/ini.core.php#ini.include-path

 

if you set that to the directory that the script runs in (or the $_SERVER['DOCUMENT_ROOT'] value) then you can use relative includes and it's all good ;)

 

 

UPDATE

<?php set_include_path( get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] ); ?>

then include as usual :D

Hello, I remember running into this problem years ago. As far as I can remember the only way I could solve it was by using an absolute link

yap...same with me...i usually use an absolute link...like below

first of all, you should define the link like this

<?
define("ABSOLUTE_LINK" , "http://www.majordisaster.com"); // for example

?>
<link rel="stylesheet" type="text/css" href="<?=ABSOLUTE_LINK;?>/style.css" />

I tend to use a baseUrl - pretty much what Tobi said.

 

In a common php file (setup.php or config.php maybe - include/required on all pages):

$baseUrl = '/my/base/app/folder';

 

Where the CSS is needed:

<link ... href="<?php echo $baseUrl; ?>/styles/screen.css" />

 

This could of course be a constant instead of a variabl.

 

Another thing to note is this will not work for includes, just from the HTML side for CSS and images etc. I would assume the includes would be handled on a per-page basis, as shown previously.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.