Define function not working

Hello,

config.php

define("BASE_PATH","/demo1");

index.php

include ($_SERVER["DOCUMENT_ROOT"] . "/config.php");
echo $_SERVER["DOCUMENT_ROOT"] . BASE_PATH;

Output

/home/starlig1/public_htmlBASE_PATH

Whats wrong guys? why the defined path is not printed? i am using php 7. also tested in 5.6. resut same.

I just tried it using Ubuntu 18.10 on my localhost - the following and it works OK?

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', 'true');


define("BASE_PATH","/demo1");
# index.php

# include ($_SERVER["DOCUMENT_ROOT"] . "/config.php");
echo $_SERVER["DOCUMENT_ROOT"] . BASE_PATH;

echo '<pre>';
	print_r($_SERVER);
echo '</pre>';

echo phpversion();
echo '<hr>';

DIE;

Output:

Edit:
Added output

Are you sure config.php is in the DOCUMENT_ROOT?

It’s generally better to get the path from the current file and traverse from there, as that is independent of the web server used.

require(__DIR__ . '/config.php');

Special Thanks to both of you.
your answers led to finding the issue.

  1. i added the error reporting lines to the script shown by @John_Betong
error_reporting(-1);
ini_set('display_errors', 'true');

Warning: include(/home/starlig1/public_html/config.php): failed to open stream: No such file or directory in /home/starlig1/public_html/demo1/index.php on line 5

So now we have the answer. i had kept the config.php file in demo1 folder itself.
That was the issue. So @rpkamp yes just fixed.

Thanks guys :))

1 Like