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

How to stop Visual Studio caching Imported MSBuild scripts?

visual-studio msbuild

I have a csproj file which references a shared MSBuild script with an <Import> directive. I have noticed that when I change the shared script, I need to close and reopen Visual Studio before it

Moving a list of ints to the front of a list

c# linq list sorting

I have a List of Int64 (List A) that needs to moved infront of another List (Int64) (List B).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.