How come non-globals from included files are usable in relative urls and unusable in full urls of included php files?

Go To StackoverFlow.com

0

when I include vars.php:

$var='hello';

in index.php:

require 'vars.php';
echo $var;

it works great, even though $var is not global. but when index.php looks like this:

require 'http://site.com/vars.php';

echo $var won't work. How can I make it work anyway?

2012-04-03 20:03
by Roy
including through http is like your visiting that page with your web browser, the php is processed before it gets to Apache and the web - NoName 2012-04-03 20:09
$var is globa - miki 2012-04-03 20:10


1

You can't. require 'http://site.com/vars.php'; tries to get the output of the PHP script as rendered by the web server at site.com - it is no longer PHP code. You cannot get a web server to return PHP source code in this way, and you shouldn't try.

Why do you want to do this? It is better to use relative paths in includes when possible. That way you can move your application code, or deploy it on a different server, without having to edit hard-coded path names.

Anyway, I think this is probably what you want to do instead (using an absolute filesystem path rather than a relative path):

require '/path/to/site.com/files/vars.php';

On Linux, this directory name is often /var/www. On Windows, the path will start with a drive letter and a colon.

It's important to understand that your statement require 'vars.php'; is not a relative URL - it is a relative filesystem path.

2012-04-03 20:09
by jnylen
And what should I replace "/path/to/site.com/files/" with, when vars.php is in "c:\site\"? (writing this path doesn't work - Roy 2012-12-01 13:56
Try require 'c:/site/vars.php'; instead. I don't use Windows but that should work. (Backslashes have special meaning in double-quoted strings. - jnylen 2012-12-04 15:23
Ads