when I include vars.php:
require 'vars.php'; echo $var;
it works great, even though $var is not global. but when index.php looks like this:
echo $var won't work. How can I make it work anyway?
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):
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