1

I'm using this code to get HTTP response:

$url = "http://example.com/";
$headers = get_headers($url);
$header = substr($headers[0], 9, 3);
echo = $header;

which works great but I need to hide referrer so I am using http://href.li/ like so:

$url = "http://href.li/?http://example.com/";
$headers = get_headers($url);
$header = substr($headers[0], 9, 3);
echo = $header;

which results in a 200 OK HTTP status code no matter what the status of the final page is. Is it possible at all to get header from the final page? Or is there another approach to accomplish this while still using a referrer hiding service?

pilsetnieks
  • 10,330
  • 12
  • 48
  • 60
Xperplay
  • 289
  • 1
  • 3
  • 14
  • The `href.li` site doesn't send a redirect. It sends a web page that contains a `` tag and Javascript that calls `window.location.replace()`. I don't think it even tries to connect to the target site, so it can't report on success or failure. – Barmar May 04 '13 at 22:17
  • Can you hide the referer by using curl? – Barmar May 04 '13 at 22:20
  • edited the title, not familiar with curl – Xperplay May 04 '13 at 22:20
  • Look it up in the PHP manual, it's the usual way to perform HTTP operations when you want detailed control. – Barmar May 04 '13 at 22:22

1 Answers1

1

You don't need a third-party service to hide your referer in this kind of request, you can set it to whatever you need, however, you cannot do it with the get_headers function. You could do it with file_get_contents and stream_context_create. In your case, I imagine it would be something like this:

$URL = 'http://example.com/';
$Context = stream_context_create(array(
    'http' => array(
        'method' => 'HEAD',
        'header' => 'Referer: '
    )
));
file_get_contents($URL, false, $Context);
$ResponseHeaders = $http_response_header;

$header = substr($ResponseHeaders[0], 9, 3);
echo $header;

stream_context_create creates a stream context with whatever options for the request that you specify and file_get_contents does the request using that context. Afterwards the script checks $http_response_header which is a kind of a magic variable that is populated after HTTP requests to HTTP URLs and it is available only in the local scope, i.e., right after you call the function making the HTTP request but not, say, in another function that gets executed afterwards. The content of $http_response_header is basically the same as the result of get_header() function.

(You can also use GET in the above example but if you don't need the response body, HEAD just gets the headers without the body. By the way, PHPs get_headers issues a GET request that fetches the whole response including the body which then gets discarded. But sometimes the receiving server or script cannot handle HEAD requests or does some actions differently compared to a GET request - you should really try it out and see what works for you.)

pilsetnieks
  • 10,330
  • 12
  • 48
  • 60
  • hiding the referrer this way seems to work and I am getting the correct HTTP status code, however I am getting the following PHP error: Warning: file_get_contents(http://403example.com/): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden – Xperplay May 04 '13 at 22:53
  • In that case, try it with a `GET` request (just change the `HEAD` to `GET`,) maybe the servers or script you're calling cannot handle it. – pilsetnieks May 04 '13 at 23:00
  • Then it seems that the either the other server is blocking requests from you or just doesn't like the empty `Referer` header. Either way, it's a valid HTTP request that, in general, shouldn't cause any issues. Try putting some other URL, that you don't mind sharing with the receiving server, in the Referer header (the `'http' => 'Referer: '` line) and see how that goes. – pilsetnieks May 04 '13 at 23:13