2

I came across an issue where an absolute path set in header location wouldn't work but pointing to the file itself did. This only affected a few customers. One of them was nice enough to try connecting through a VPN which made the header location work.

Didn't work:

header('Location: http://www.example.com' . $_SERVER['PHP_SELF']);

Works:

header('Location: ' . $_SERVER['PHP_SELF']);

Can anyone shine some light on this?

Thanks

Bastien
  • 189
  • 1
  • 3
  • 18
  • 1
    Maybe [this](http://stackoverflow.com/a/3520193/2229404) could shine some light? – Orel Biton Aug 19 '14 at 19:33
  • 1
    The one that doesn't work is actually correct. As for why it doesn't work - did you maybe gather any info on which browser produced the fault? – N.B. Aug 19 '14 at 19:33
  • Yes, tried 3 different browser, tablet, cell phone. None worked. Connected through VPN, worked instantly. – Bastien Aug 19 '14 at 19:36
  • 1
    Can everyone reach the http://www.example.com (or whatever it actually is) URL? Or just over the VPN? Is the URL reachable outside of your local network? – TunaMaxx Aug 19 '14 at 19:39
  • It works fine for 99% of the customers. – Bastien Aug 19 '14 at 19:43

2 Answers2

1

Your affected customers are not able to resolve the http://www.example.com (or whatever it actually is) URL for some reason. You can verify this by having them just try and visit the http://www.example.com by manually typing it in the browser location bar. That should fail too.

This can happen you have a site that is available under a number of domains, or directly by the IP address. Even www / non-www versions can make this happen. They hit the site at one domain or IP address that works for them, and then you try and redirect them to a URL they can not resolve. This explains why redirecting to just the path works, but an absolute URL doesn't.

If they can reach http://www.example.com in the browser, but not by redirect, ask them to blow out the browser cache.

TunaMaxx
  • 1,782
  • 12
  • 18
  • _especially_ with the mentioning of it working over VPN this is most likely the answer (which can caused by either a DNS problem, routing problems, or incorrect firewall settings at the servers side). If those aren't it (and my main reason for commenting): I experienced this problem recently with virtual machines under libvirt version 0.8X (denying access to seemingly random IPs, but consistently for only those IPs), for which an upgrade to 1.* of libvirt fixed the problem. – Wrikken Aug 19 '14 at 20:04
  • My thoughts exactly; the "works over VPN" part is what solidified it for me. As for the *reason* why some folks cant hit the URL, I purposely didn't elaborate, but you are correct in that it could be any number of things. – TunaMaxx Aug 19 '14 at 20:10
  • Thanks for the reply. Cache had been cleared many times and the problem remained. – Bastien Aug 20 '14 at 14:18
  • Where do you guys suggest at look first? I am out of ideas. – Bastien Aug 20 '14 at 14:19
  • Can the affected users reach the URL manually in a browser? What URL/IP are they using before the header redirect fails? Don't assume, check with them. – TunaMaxx Aug 20 '14 at 19:42
0

Also always exit the script afterwards because otherwise in my experience in certain circumstances code that comes after the redirect might still be executed. So a good example would look like this:

header("location:http://www.example.com/path/to/myfile.php");
exit;

Often you would use a server variable for this case:

$url = $_SERVER["HTTP_HOST"]."/path/to/myfile.php";
header("location:".$url);
exit;

Cheers!

Link answer: https://tousu.in/qa/?qa=1091514/