Hi everyone,

I have a controller file that consists entirely of php. It just queries the database and then it includes another template file.

This template file has the following at the top:

 <?php
if ($_SERVER['HTTP_HOST'] != "new_site.com") { 
   define ('__ROOT__', $_SERVER['DOCUMENT_ROOT'] . '/new_site');
} else {
   define ('__ROOT__', $_SERVER['DOCUMENT_ROOT']);
}

include_once(__ROOT__ . "/includes/helpers.inc.php");
include_once(__ROOT__ . "/includes/config.inc.php");
 ?>

When I test the template page in the browser, I looked in the source code and saw the following error:

Notice: Constant ROOT already defined in /Applications/MAMP/htdocs/new_site/consumer/catalogue/catalogue.php on line 3

Line 3 is the following from the above code:

define ('ROOT', $_SERVER['DOCUMENT_ROOT'] . '/new_site');

Does anyone know why I'm getting this error?

Appreciate any help.

    This can happen with functions and constants
    when you include the same 'config page' several times.

    one constant can not be defined 2 times.
    So second time you include or load such a page, you will get this error.

    There are 2 solutions, at least.

    1. Organize your pages such as configs/defines are only run once.
      For example use: include_once 'config.php'

    2. Use a test to see if ROOT is already defined
      Using this [man]defined/man function

       <?php
      
      if(!defined('__ROOT__'){ // can not be defined 2 times
      	if ($_SERVER['HTTP_HOST'] != "new_site.com") {
      	   define ('__ROOT__', $_SERVER['DOCUMENT_ROOT'] . '/new_site');
      	} else {
      	   define ('__ROOT__', $_SERVER['DOCUMENT_ROOT']);
      	}
      }
      
      include_once(__ROOT__ . "/includes/helpers.inc.php");
      include_once(__ROOT__ . "/includes/config.inc.php");
      
      ?> 

      Thanks for the reply,

      I tried running your defined() function but I got the following error:

      Parse error: syntax error, unexpected '{' in /Applications/MAMP/htdocs/new_site/business/index.php on line 2

      Line 2 is:

      if(!defined('ROOT'){ // can not be defined 2 times

      Any ideas why this is occurring?

        That is just due to a typo error, i.e., this:

        if(!defined('__ROOT__'){

        should be:

        if(!defined('__ROOT__')){

          Thanks again - all fixed now.

            Write a Reply...