March 19, 200818 yr 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.
March 19, 200818 yr 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.
March 19, 200818 yr 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!)
March 20, 200818 yr 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.
March 20, 200818 yr 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
March 20, 200818 yr 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 ?!
March 20, 200818 yr 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.
March 20, 200818 yr 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
March 24, 200818 yr 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" />
March 25, 200818 yr 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