views:

109

answers:

4

I've inherited a bad sitation where at our network we have many (read: many) hosted sites doing this:

include "http://www.example.com/news.php";

Yes, I know that this is bad, insecure, etc, and that it should be echo file_get_contents(...); or something like that (the output of "news.php" is just HTML), but unfortunately this is what they use now and we can't easily change that.

It used to work fine, until yesterday. We started to 301-redirect all www.example.com requests to just example.com. PHP does not appear to follow this redirect when people include the version with www, so this redirect breaks a lot of sites.

To sum it up: Is there a way to get PHP to follow those redirects? I can only make modification on the example.com side or through server-wide configuration.

A: 

Let's start with the redirecting http repsonse.

<?php
error_reporting(E_ALL);
var_dump( get_headers('http://www.example.com/news.php') );
// include 'http://www.example.com/news.php'

The output should contain HTTP/1.0 301 Moved Permanently as the first entry and Location: http://example.com/news.php somewhere.

VolkerK
Yes, but I can't change that code, or I would just change it to file_get_contents. I have to do this on the example.com side.
Bart van Heukelom
...or through a server-wide PHP setting. Well, I _can_ go and change all the includes, but it'd just be a lot of work :p
Bart van Heukelom
It just a test script. Place it in a/any directory where the `include 'http://www.example.com/news.php'`doesn't work as expected and examine the output.
VolkerK
And if `http/1.x 310 ...` is not the first line or if there's no `Location: xyz` line in the result that's what is preventing php from following the "redirect" (because it's _not_ a redirect). If it _is_ there we have to check the more unlikely causes (whatever they may be).
VolkerK
A: 

I don't think any of those solutions provided by PHP itself would help... I just don't think any of them follow headers and what not. For what it's worth, I do think, though, that this behaviour is correct: you're asking for the result of a certain request and you got it. The fact that the result is telling you to look elsewhere is, in and of itself, a valid result.

If I were you, I'd look at cURL. There's a PHP extension for it and it will allow you to tell it to follow headers and get to where you're trying to get. If this is not usable (as in, you absolutely, positively have to use the approach you currently are), you will need to revert the redirects on the 'source' server: maybe you could have it return the information or the redirect based on requesting IP address or something similar?

Narcissus
The http fopen wrapper does follow `Location: xyz` headers unless told otherwise (e.g. by get_headers())
VolkerK
A: 

Can't you use curl? In curl_setopt it has an option to follow redirects.

AntonioCS
I guess the answer is "no" because of "but unfortunately this is what they use now and we can't easily change that" ;-)
VolkerK
Ops, missed that :P
AntonioCS
Well he does say 'can't ||easily|| change that'. So it's not impossible to change it
AntonioCS
+2  A: 

You said, in a comment: "I can go and change all the includes, but it'd just be a lot of work".

Yes. That's the "bad, insecure, but-I-don't-have-a-reason-to-change-it code" coming back to bite you. It will be a lot of work; but now there is a compelling reason to change it. Sometimes, cleaning up an old mess is the simplest way out of it, although not the easiest.

Edit: I didn't mean "it's your code and your fault" - rather, "bad code is often a lot of work to fix, but it's usually less work than to keep piling hacks around it for eternity, just to keep it kinda-working".

As for "going and changing it", I'd recommend using cURL - it works much better than PHP's HTTP fopen wrappers.

Piskvor
Well in my defense, I did not make up the bad code. I inherited it :(
Bart van Heukelom
@Bart van Heukelom: Sorry, I was too dismissive in the original version. Didn't mean to sound so arrogant.
Piskvor