Accessing $vars from external php files through require
Moderator: General Moderators
Accessing $vars from external php files through require
Hello everyone, I am having a hard time making a website available in my localhost.
The website is on-line, up and running and most of the pages include php files, for example: require $dir_site."/lib/db.obj.php"; I am just trying to recreate it on my localhost.
$dir_site is a variable and is defined in a file named config.obj.php; that file is then required in all pages to make it easier to type in certain paths.
Now the thing is, on my localhost, when I include the config.obj.php file that has the defined $dir_site = "http://..." (and many other important vars) and try to access $dir_site var, it is empty, so I can't include/require basic files.
I don't remember having trouble using $vars in file1.php that were defined in file2.php, as long as I included the file2.php...
Could it be some php.ini configuration setting that differs from the webserver where the website is hosted?
I am running XAMPP with PHP 5.1.6.
Thanks in advance.
The website is on-line, up and running and most of the pages include php files, for example: require $dir_site."/lib/db.obj.php"; I am just trying to recreate it on my localhost.
$dir_site is a variable and is defined in a file named config.obj.php; that file is then required in all pages to make it easier to type in certain paths.
Now the thing is, on my localhost, when I include the config.obj.php file that has the defined $dir_site = "http://..." (and many other important vars) and try to access $dir_site var, it is empty, so I can't include/require basic files.
I don't remember having trouble using $vars in file1.php that were defined in file2.php, as long as I included the file2.php...
Could it be some php.ini configuration setting that differs from the webserver where the website is hosted?
I am running XAMPP with PHP 5.1.6.
Thanks in advance.
Re: Accessing $vars from external php files through require
By the way I have just tested having 2 php files, 1 is a standard index.php with the HTML tags and an echo $var;
The other contains the value from $var;
The page outputs the $var value correctly, even though it is written in an external .php file.
The other contains the value from $var;
The page outputs the $var value correctly, even though it is written in an external .php file.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Accessing $vars from external php files through require
The issue is one of two things. Either $dir_site is not defined or you are not including the correct file. In order to find out if it is the latter, you can check this by adding an echo into the config.obj.php file. If that echo shows up in the including script, then that is not your problem. In that case, it may be the former. To check that, use PHP's get_defined_vars() function and use print_r() on the resulting array. $dir_site should show up inside of this.
Re: Accessing $vars from external php files through require
Hey there, thanks for your reply, I forgot to mention, but I have ruled out the chance that the file was not being included by adding an echo 'Config.obj.php included'; line to my target file.
I'll read up on the functions you mentioned and report the outcome.
I'll read up on the functions you mentioned and report the outcome.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Accessing $vars from external php files through require
Also, I just realized that you said that you were including files using an absolute path starting with the http protocol. Doing so may make your server think that you are attempting to include remote files, in which case it would get the resulting output of your files and not the raw code. You may want to look into setting the default include path using PHP's set_include_path() function. If you use it with dirname(__FILE__), you can remove the need to append a path to your include and require directives.
Re: Accessing $vars from external php files through require
Hm, I think you found the problem.
The outcome of the print_r(get_defined_variables()) returns no $dir_site.
And yes, everytime I developed a website I always use relative paths like /dir, or ../dir, etc...
So probably PHP is reading the outcome of config.obj.php like files, with no content, and gets nothing out of it...the thing is, this whole huge webpage uses those thousands of vars included in the config file, and on the server they do point to http:// paths, and if I change all absolute paths into relative paths I will take a year
Is there a workaround so that the PHP engine reads the real php instead of the outcome? This may be dumb, but could ftp:// work?
I'll keep on reading up, thanks again for your tips.
The outcome of the print_r(get_defined_variables()) returns no $dir_site.
And yes, everytime I developed a website I always use relative paths like /dir, or ../dir, etc...
So probably PHP is reading the outcome of config.obj.php like files, with no content, and gets nothing out of it...the thing is, this whole huge webpage uses those thousands of vars included in the config file, and on the server they do point to http:// paths, and if I change all absolute paths into relative paths I will take a year
Is there a workaround so that the PHP engine reads the real php instead of the outcome? This may be dumb, but could ftp:// work?
I'll keep on reading up, thanks again for your tips.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Accessing $vars from external php files through require
That's what set_include_path solves. You have a central file like index.php that all requests are directed through, or a file like config.php that every file must include. If you are going the index.php route, then you can just use dirname(__FILE__) as the include_path in index.php and use the include and require directives as if they are always relative to that path. If you are going the config.php route, then use dirname(__FILE__) as the include_path in config.php, and each file would only need to use relative paths to refer to config.php at the top of the file. After that, they would use the include and require directives as if they were relative to the include_path.
Re: Accessing $vars from external php files through require
At long last!
I didn't have to use the set_include_path() but you were of great help. I have changed a few core includes, this time using relative paths instead of http:// and now PHP is reading the .php file and not the HTML outcome, so I have the website running on my localhost, although it still needs a bit of tweaking, but I'm on the right path.
Thanks once again!
I didn't have to use the set_include_path() but you were of great help. I have changed a few core includes, this time using relative paths instead of http:// and now PHP is reading the .php file and not the HTML outcome, so I have the website running on my localhost, although it still needs a bit of tweaking, but I'm on the right path.
Thanks once again!
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Accessing $vars from external php files through require
Oh. I thought you were saying that you didn't want to do that. Good to hear that it's working though. 