0

I'm trying to include a file with an absolute url:

<?
session_start();
$_SESSION['sr_path'] = 'http://domain.org/www/myapp';

include($_SESSION['sr_path'].'/assets/contact.php');
?>

But it don't work.

Any idea why please ?

4
  • you have to use absolute path of include file, not the url. Commented Feb 21, 2016 at 15:05
  • If I move to a https protocole after do I need to change the path ? Commented Feb 21, 2016 at 15:06
  • No. the path is the local path. Nothing to do with http, https, etc... Commented Feb 21, 2016 at 15:08
  • You cannot include a php file from "outside". It must reside locally on your drive somewhere. Commented Feb 21, 2016 at 15:08

2 Answers 2

1

you include your files by directories not url:

include(dirname(__FILE__).'/assets/contact.php');

or

include(dirname(__FILE__).'/contact.php');

where dirname(__FILE__) get the path of your current file you write this code in it.

or for all case you can define constant in your website index.php page, and use this const anywhere:

define('ROOT', dirname(__FILE__));

then use it in any dir like this:

include(dirname(__FILE__).'/same_index_file_path/contact.php');
Sign up to request clarification or add additional context in comments.

1 Comment

This will work only if the included file is below same directory of calling file (not executing file).
1

You are currently attempting to include a file by a URL not by the path on the server. By default you cannot use the 'HTTP' wrapper or you'll get this warning (or similar): http:// wrapper is disabled in the server configuration by allow_url_include=0

Whilst you can include files by URL, it is generally preferred to include with a path on your server.

If you did want to go ahead with your current method and include something which is stored on a different server to the one the script is running on, you'll need to update your configuration by setting 'allow_url_include' to 1.

The PHP Docs for include specify:

If "URL include wrappers" are enabled in PHP, you can specify the file to be included using a URL (via HTTP or other supported wrapper - see Supported Protocols and Wrappers for a list of protocols) instead of a local pathname.

It's worth noting, however, that if you use HTTP you could be relying on a remote server to process the PHP script for you and return it to your script. As the docs state:

Remote file may be processed at the remote server (depending on the file extension and the fact if the remote server runs PHP or not) but it still has to produce a valid PHP script because it will be processed at the local server.

It goes onto clarify:

...the script is actually being run on the remote server and the result is then being included into the local script.

TL;DR

I would guess what you actually want to do, assuming the script you are attempting to include is on the same server as the script that is doing the including, is to just give the path to it on your server such as: /var/www/myapp/assets/contact.php which would look like this:

include('/var/www/myapp/assets/contact.php');

instead of this:

include('http://example.com/myapp/assets/contact.php');

1 Comment

This may confuse OP: you have also to explain the difference between include a file and include a url...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.