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?
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.
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