View Full Version : Problem with Includes and Constants

08-29-2007, 03:16 PM
In my config file I have a constant that I define called PATH which shows the root path for the rest of the site.

define('PATH', 'http://mysite.com/');

I then include my header in the main page like this.

include (PATH.'includes/admin/header.php');

in my header.php i use the constant again for the images like this

<img src="<?php echo (PATH); ?>images/portal_05.jpg" width="19" height="79" alt="">

When I open the page though I get the following error:

PHP Notice: Use of undefined constant PATH - assumed 'PATH' in C:\DATA\mysite.com\htdocs\includes\admin\header.php on line 2

I am kinda new to using constants and this one is really throwing me. Any help would be greatly appreciated.

08-29-2007, 03:19 PM
you need to include file where constant PATH is defined in header.php

08-29-2007, 03:41 PM
Okay, that worked on my header, footer, but I am still having issues with one file. I have a PHP file that has all the styles in it and it isn't working for those. All the text and everything is in there correctly from the stylesheet, but when I use a background image like this:

background-image: url("<?php echo (PATH); ?>images/portal_14.jpg");

it just shows the word path in the pathname and not the URL. I tried including the config file in that one, but it didn't change anything. I am not getting an error though, but it is just showing the pathname as the word path.


08-29-2007, 03:43 PM
Nevermind... I had lowercase path instead of uppercase path

Thank you so much for your help!!!

08-29-2007, 03:59 PM
The reason why your included file(s) does not have access to any variable/constant that you defined in your config file, is because you are including the include file using a http:// request -

// your config file -
define('PATH', 'http://mysite.com/');
// your main file -
include (PATH.'includes/admin/header.php');When you use the http://mysite.com/ to specify the include file, PHP makes a separate http request back to your web server and the included file is parsed by php in a separate process, the same as if you had browsed to that file. Your main file only receives any output from that included file instead of actually including the PHP code that is in it.

To include files locally, you need to access them through a file system path and not a http request.

08-29-2007, 04:33 PM
Why not use $_SERVER['DOCUMENT_ROOT'];?

08-29-2007, 05:53 PM
Why not use a <base> element and specify your CSS paths as relative?