-1

I have a problem with an URL-include, which I don't understand...: For testing I have coded the following script:

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
echo "First text";
include("http://www.xxxxxxxxxx.de/includetest.php");
echo "Second text"; 
?>

Allow_url_include is set to on. (via php.ini)

Allor_url_fopen ist set to on. (via php.ini)

The includetest.php only contains plain text for testing. There is no php-code.

The result of that script is only the "first text". After that the script is stopped.

If I use "or die('not working');" after the include, the result is the whole text (also the second text) with the following warning:

Warning: include(1): failed to open stream: No such file or directory in /srv2/www/htdocs/xhtml-test/_baustelle/testphp02.php on line 6 Warning: include(): Failed opening '1' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /srv2/www/htdocs/xhtml-test/_baustelle/testphp02.php on line 6

Why is that? I am at a loss...

Julian
  • 127
  • 1
  • 5
  • For `or die` to work you need to use proper grouping: `(include '..') or die(..)`, otherwise precedence will cause a different interpretation. – deceze Feb 23 '16 at 09:30
  • 1
    Possible duplicate of [Full URL not working with php include](http://stackoverflow.com/questions/13369529/full-url-not-working-with-php-include) – Glapa Feb 23 '16 at 09:31
  • The problem is not the "or die". The problem is the stopping of the script. – Julian Feb 23 '16 at 10:05
  • The possible duplicate doesn't help me. sorry... – Julian Feb 23 '16 at 10:05

2 Answers2

0

Here is the problem of code:

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

Ref. of this Is Here

The file you are including is not a valid php file as it is already surved by a server as php.

This code should work as you want:

<?php
error_reporting(E_ALL|E_STRICT);
ini_set('display_errors', 1);
echo "First text";
echo file_get_contents("http://www.xxxxxxxxxx.de/includetest.php");
echo "Second text"; 
?>
Masum Nishat
  • 167
  • 3
  • 15
  • Thanks @masum-nishat file_get_contents works, that's right. but my code (with include) works also at many other clients. only at the one client it doesn't work. i guess that's a problem with the server. but what? – Julian Feb 23 '16 at 10:45
  • I dont know what is going on. But you can check something: 1. site is on `http` or `https` 2. Manually check availability of url, 3. Try connecting some other url and see the error log. hope this will help – Masum Nishat Feb 23 '16 at 11:02
0

You should use Relative paths in PHP include function.

include '/path/to/file.php';  // You can include file by relative path

As per documentation,

include through HTTP

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. If the target server interprets the target file as PHP code, variables may be passed to the included file using a URL request string as used with HTTP GET. This is not strictly speaking the same thing as including the file and having it inherit the parent file's variable scope; the script is actually being run on the remote server and the result is then being included into the local script.

/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, 'Works' here means that the variables
* $foo and $bar are available within the included file. */

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'file.txt';  // Works.
include 'file.php';  // Works.

Warning

Security warning

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. If the file from the remote server should be processed there and outputted only, readfile() is much better function to use. Otherwise, special care should be taken to secure the remote script to produce a valid and desired code.

Here is understanding of Paths.

1) Relative Paths

index.html
/graphics/image.png
/help/articles/how-do-i-set-up-a-webpage.html

2) Absolute Paths

http://www.mysite1.com
http://www.mysite2.com/graphics/image.png
http://www.mysite3.com/help/articles/how-do-i-set-up-a-webpage.html

The first difference you'll notice between the two different types of links is that absolute paths always include the domain name of the website, including http://www., whereas relative links only point to a file or a file path. When a user clicks a relative link, the browser takes them to that location on the current site.

For that reason, you can only use relative links when linking to pages or files within your site, and you must use absolute links if you're linking to a location on another website.

For more information, Refer this link also.

Hope it will help you :)

Community
  • 1
  • 1
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • Yes, I can use absolute paths in include functions. for using http-includes it is necessary to set the allow_url_include attribute to on. and this is done! My script works at many other sites. But in this one case at this one customer it doesn't work. – Julian Feb 23 '16 at 10:58
  • @Julian: Yes. But in some case. Please see example I have included in answer. – Ravi Hirani Feb 23 '16 at 11:02