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

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?

Answers


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.


Need Your Help

Search and display results from SQL database

php html mysql sql database

I have an MySQL database and I need to search for either the transaction number (and it shows me my name, email and item_name) or search for my email address AND name AND date of purchase which wil...