Loading variables in external folder: only works once
I'm successfully using the following script to 开发者_StackOverflow中文版load session variables within other folders on my site:
<?php
require('../includes/configure.php');
ini_set('include_path', DIR_FS_CATALOG . PATH_SEPARATOR . ini_get('include_path'));
chdir(DIR_FS_CATALOG);
require_once('includes/application_top.php');
?>
This script allows me to load my header template right after, which includes the variable output that I want:
<?php
include('includes/templates/header.php');
?>
So, the thing is this script works flawlessly in three other scenarios (CMS systems)... For this particular one I'm working on, it only works the first time. It does successfully load my header and variables correctly, however the next time the page load it won't work.
Can anyone provide an explanation / solution? Thanks!
As you answered to a comment that "it won't work" means "I get a blank page", the first thing you should do is enable PHP's error reporting. Put at the beginning of your script:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On'); // shows in the web page instead of just logs
Then your page probably won't stay blank and you'll have useful error messages.
Second, you should replace your include
by a require
. This way, if PHP cannot find the file, you'll get a fatal error. Depending on the content, a require_once
could be even better suited.
Then, you should be cautious with relative paths in includes. They are not immediately relative to the script where the include is. PHP first tries to use the include path and the path of the calling script (the file that was launched first for this HTTP query). It is recommended to use a full path :
require dirname(dirname(__FILE__)) . '/includes/configure.php';
require dirname(__DIR__) . '/includes/configure.php'; // PHP5.3 only
One last thing: don't use the expression session variables when you're dealing with application constants. "Session" has a special meaning in PHP, and in web development in general.
精彩评论