1

I use require_once("header.php"); for all pages of my website. Though, I feel like I can only use that code for files filed directly under the public_html folder. If I use the require_once("header.php"); code for any other files located in a directory (e.g. shirts) under public_html, such that you have public_html/shirts/example.php, the header.php doesn't load the images, links, or any other pages correctly.

I know to resolve this in the short term, I can make a new header just for that directory and add ../ to all the code, but I think there's an easier way. Does anyone know of a way I can just change the code of header.php, so I don't have to make separate headers?

Thanks.

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
Andrew
  • 11
  • 2
  • This is the difference between relative and absolute paths. But you need to understand paths (according to platform): http://en.wikipedia.org/wiki/Path_%28computing%29 – Jared Farrish Sep 22 '11 at 23:56
  • 1
    http://stackoverflow.com/questions/1755216/php-relative-and-absolute-paths – Jared Farrish Sep 22 '11 at 23:57

1 Answers1

0

You could add to your include_path.

Either add the current directory where header.php is, or create a folder called includes somewhere in your site and add that to the include path.

You can either update the include_path in php.ini (easiest) or do it programmatically like this:

set_include_path(implode(PATH_SEPARATOR, array(
    '/path/to/your/includes',
    get_include_path(),
)));

The programmatic method works well, but unless most of your files are served through a common script (i.e. front controller pattern) then you will need to copy that code to all of your files which is not ideal. Sometimes you can use .htaccess to set the include_path as well, but only if PHP is an Apache module.

As long as your structure is fairly static, there is nothing wrong with includes using ../ either.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • I updated the include_path in php.ini, but the individual page now doesn't recognize header.php in include_path = ".:/home/boldfo5/includes". Is there a way to fix this? – Andrew Sep 23 '11 at 14:31
  • Make sure you restart the webserver after changing php.ini for the changes to take effect. What do you mean the individual page doesn't recognize header.php? Are some pages recognizing it? Since you added that to include path, you should be able to `include "header.php"` from any of your scripts without any path information and if it isn't found in the current directory, it will look for it in /home/boldfo5/includes. – drew010 Sep 23 '11 at 16:47