1

OK, the title is a little confusing but this is the structure of my web site:

/
|--header.php
|--page1.php
+--css
   |--style.css
+--subsection
   |--page2.php
+--functions
   |--classes.php

Right, so both page1.php and page2.php will include header.php. This I do by just using the relative file path from whatever php page to the header.php.

header.php itself includes other files; for example css/style.css and functions/classes.php.

I might include them like the following:

<link rel="stylesheet" href="css/style.css">

<?php require_once("functions/classes.php"); ?>

This will work for page1.php as the reletive paths are correct. For subsection/page2.php this will fail as the paths should be ../css/style.css and ../functions/classes.php but remain as they are defined in header.php.

My question is how can I get header.php to always use the correct relative file paths for its includes regardless of where the file calling header.php (e.g. apage.php) is located in the web site directory.

harryg
  • 23,311
  • 45
  • 125
  • 198
  • You can also use ajax to load content of page2.php from page1. – Jay Shukla Jul 18 '13 at 11:24
  • I really think you've misunderstood the question – harryg Jul 18 '13 at 12:10
  • thats why it is good practice to always define the root path in your config or default file, as **JohnnyFaldo** said below. if that is not possible then always use paths relative to `$_SERVER["DOCUMENT_ROOT"]` – user1402647 Jul 18 '13 at 12:32

4 Answers4

1

Set a base path to the css /functions :

define('CSS_BASE','insert/full/path/here');

then access css in header.php using

<link rel="stylesheet" href="<?=CSS_BASE;?>/style.css">
JohnnyFaldo
  • 4,121
  • 4
  • 19
  • 29
  • this is probably the best answer, although I ended up just using a variable: `$parent_dir = "http://" .$_SERVER['SERVER_NAME'] ."/web_dir";` – harryg Jul 18 '13 at 13:06
  • fair enough, I like to define a few constants at the top of apps for things like this, there are some advantages: http://php.net/manual/en/language.constants.php – JohnnyFaldo Jul 18 '13 at 13:11
0

When I did this I set a variable (say $urlStart) at the start of each page (page1.php, page2.php) that contained the relative path of the main directory (eg page1.php would have $urlStart='./', page2.php would have $urlStart='../'). Then in your header.php, use this variable at the start of each URL. Like this:

<link rel="stylesheet" href="<?php echo $urlStart;?>css/style.css">

<?php require_once($urlStart . "functions/classes.php"); ?>
jacobianism
  • 226
  • 1
  • 3
  • 12
0

For php includes, making the path relative to header.php is ok.

For the css file, you should use an absolute path, like <link rel="stylesheet" href="/css/style.css">.

Edit : The css/javascripts files are not "include" ; those are html tags, so they will be processed client-side by the browser. That's the reason why the path should be relative to the url of the page where the html will be, not to its place on the server.

Hugehornet
  • 181
  • 7
-1

If you are including page2.php inside the page1 then your path will work. no need to go back to the previous folder.

Jay Shukla
  • 5,794
  • 8
  • 33
  • 63