"Hotlinking is Forbidden" when using file_get_contents()

Asked

Viewed 110 times

3

This error appears when I try to use the function file_get_contents() of PHP:

failed to open stream: HTTP request failed! HTTP/1.1 403 Hotlinking is Forbidden

As a parameter we have a normal PHP page, which makes some connections to the client’s database and an internal one of it. This target has not header none. Depending on what happens inside (it all works out), it will "printar" (with echo) a JSON. And that’s what I want to acquire by my file_get_contents. My hotlink is disabled.

What can I do to fix?

  • I am the server administrator.

  • Good. In Target we have a normal PHP page, which makes some connections to the client’s database and an internal one of it. This Target has no header. Depending on what happens inside ( all right), it will sharpen ( with echo ) a JSON. And that’s what I want to acquire by my file_get_contents. My hotlink is disabled.

  • Describe this explanation in the body of the question, but describe it in a way that people outside the project can understand.

1 answer

5

Usually the Hotlinks are detected by header Referer sent by browser.

To specify a header in the use of file_get_contents, we have the stream_context_create:

$opts = array(
   'http'=>array(
      'header'=>'Referer: http://www.enderecodositerequisitado.com'."\r\n"
   )
);

$context = stream_context_create($opts);

$file = file_get_contents(
    'http://www.enderecodositerequisitado.com/caminho', false, $context
);

In this way, the behavior is similar to an access made by a link of the site itself. Note that the protection may be more complex than this, but in most cases, the solution is by this way.

In the above example, the important thing is that the Referer has as its origin something expected in the normal use of the site, by a user who is browsing normally.

Alternatively, instead of doing all this, it could be the case of simply putting your server IP in a white list on the server side that provides the data.

  • I decided otherwise. The staff of my lodging gave me a little help. Just put the door 8080, to start with apache. example: http://www.enderecodositerequisitado.com:8080

  • @Rafaelandersonlobo this is no solution to what was asked. If it worked in your case, it’s another problem, and it has nothing to do with the error of the question.

  • Bullshit. I posted the problem and the solution, so as the problem would not be this ?

  • 2

    Because the problem has not been solved, it has been circumvented. Solution to hotlink lock has nothing to do with door. If you had protection in the 8080 it would still work. You solved the problem of making it work, in fact. But it has nothing to do with the problem described in the question. It would be like asking "how do I remove duplicate files from my pq hard drive I need space", and then coming back here and saying "the solution is to buy another hard drive". Buying another HD solves, we know, but has nothing to do with removing duplicate files. Fixed? Yes, but you didn’t answer the original question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.