Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Problem with Includes and Constants

    In my config file I have a constant that I define called PATH which shows the root path for the rest of the site.

    Code:
    define('PATH', 'http://mysite.com/');
    I then include my header in the main page like this.

    Code:
    include (PATH.'includes/admin/header.php');
    in my header.php i use the constant again for the images like this

    Code:
    <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.

  • #2
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    you need to include file where constant PATH is defined in header.php

  • #3
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    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:

    Code:
    	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.

    Thoughts?

  • #4
    Regular Coder
    Join Date
    Jul 2004
    Location
    Tampa
    Posts
    223
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Nevermind... I had lowercase path instead of uppercase path

    Thank you so much for your help!!!

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,151
    Thanks
    2
    Thanked 335 Times in 327 Posts
    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 -
    Code:
    // 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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Why not use $_SERVER['DOCUMENT_ROOT'];?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Why not use a <base> element and specify your CSS paths as relative?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •