Search code examples
phpproxyincludeproxy-server

How to get the PHP include function to work with proxy settings?


<?php
$incfile = $_REQUEST["file"];
include($incfile);
?>

upload.php file:

<?php
$context = array(
    'http' => array(
        'proxy' => "tcp://proxy.example.com:80",
        'request_fulluri' => true,
        'verify_peer'      => false,
        'verify_peer_name' => false,
    )
);
stream_context_set_default($context);
?>

proxy.php file:

auto_prepend_file=proxy.php
allow_url_include=1

php.ini:

I browse to http://testexample.com/upload.php?file=http://example.com/file.php but http://example.com/file.php times out with error Warning: include(): failed to open stream: Connection timed out. I played with echo file_get_contents and used the URL path and that works fine as it appears to honor the proxy settings. So does anyone know what the issue might be with using include or why it does not use my proxy settings?

Edit: As a workaround I used this code below:

<?php
$incfile = $_REQUEST["file"];
$filecontent = file_get_contents($incfile);
eval($filecontent);
?>

The problem with this though is that it reads in the PHP as a string and not the whole file. So I have to remove the PHP beginning and ending tags which changes the GET request body so effects my results. So even though it kinds works, the include function is really what I need.


Solution

  • So you need your http requests for example.com to go to go through proxy.example.com. Would it suffice to simply override DNS for example.com to point to proxy.example.com - perhaps in the hosts file - on this development server? Then you could

    include 'http://example.com/file.php';
    

    If you want to limit the solution to PHP, you could define a custom stream wrapper for your proxy.

    http://php.net/manual/en/function.stream-wrapper-register.php

    http://php.net/manual/en/stream.streamwrapper.example-1.php