PHP, CURL and include files

I’m no coder so I’ll just try to describe my problem as good as possible.

For security reasons my webhost has just turned off the use of “allow_url_fopen” which means that I can no longer use php includes like this:

<?include (‘some-file.php’);?>

Instead they give access to CULR which should solve the problem. My question is with which code I shold replace the above code to make things work?

Thanks in advance :wink:

You can do that unless the file is on the remote host.

What do you want include to do? Show file contents or actually execute the code in it?

yeah
include (‘some-file.php’);

will work regardless of allow_url_fopen(), because that is a filesystem path(a relative one), and not a url.

what wont work is
include (‘http://example.org/some-file.php’);

which is usually a good thing. there is almost never a good reason to use include on a url.

I’ve had a couple of instances where I needed to get outside info from weather services and couldn’t fopen (not the same as an include, but…) The code I ended up using is

$ch = curl_init();
$timeout = 15; /* set to zero for no timeout */
curl_setopt ($ch, CURLOPT_URL, 'http://www.domain.com/file.txt');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

If you absolutely need to make an http request (which you shouldn’t normally need to do for an include) something like that should work for you.

Thank you all for your advice.

It’s seems that you all right. The problem only occurs when I include the full URL even though it’s from the same domain.

And I really need to request the full URL :eek:

what makes you think you “need” to use include on a url?

It’s because my website consists of several thousands pages and my folder structure is quite a mess. Right now I point to all my include files using the full URL (which doesn’t work anymore). So not using the full URL would require quite a lot of updating which can’t just be done with find/replace.

Please don’t ask me why I used the full URL to begin with :slight_smile: I guess that was a mistake.

Maybe not so much of a mistake, but more of a poor coding practice that “worked” on your old server. Problems arise when settings aren’t the same (short tags, magic quotes, safe mode, globals, - allow_url_fopen, etc). Don’t feel bad, I’m sure most if not all of us have our own stories we could tell. The trick is to learn from this “gotcha” and improve your coding skills. Often taking short-cuts is what has been called a “deficit”, meaning you don’t get away with it forever, sooner or later someone has to pay.

maybe you can use $_SERVER[‘DOCUMENT_ROOT’] to help you create your filepaths? if they map to a url, they should more or less the same in the filesystem unless you have done some type of url rewriting.

in any case, you shouldnt have used include() anyway. all you were including was the html output. you should have been using readfile() you can see the manual for more info as to why, it is mentioned.

regardless, you have some editing that you will need to do. those calls to include will need to be changed to either use a filesystem path, or use curl. might as well do it right and use a filesystem path.

the other option is to switch hosts if you cannot get this setting changed for you.