开发者

Hotlink redirection

I have this URL: www.example.com/yyy.gif at my site. It is actually NOT a direct link to the image but a HTML page containing the image (direct url to said image is www.example.com/files/yyy.gif). I want to keep it that way.

THE PROBLEM

When the link (www.example.com/yyy.gif) is posted somewhere (forums, comments at various websites) it is quite common that their script assumes it is a direct link to an image and tries to display it as an image (<image src="www.example.com/yyy.gif">) which leads to broken image on their site.

THE QUESTION

Is there a way to detect these cases and automatically reroute them to the direct image URL? Keep in mind that visitors should be able to open the o开发者_如何学编程riginal URL without being redirected.


You could check the $_SERVER['HTTP_REFERER'] variable to see if the url originates from your site.

If it doesn't, output some headers for the image and then the image itself instead of what you would normally do.


I think I got it!

So I checked what request headers are generated when the same URL is accessed by clinking on a link and when it is loaded using the <img> tag.

The only parameter that differs is "Accept". When the URL was accessed by <img> it was */* (Chrome, IE, Safari) or image/png,image/*;q=0.8,*/*;q=0.5 (FF).

But when accessed by clicking on a link (or just opening the URL directly), it is something like this text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

So I'm checking for 'text/html' in my script before outputting any content. If it is present, I output the html version; otherwise go for the image directly.

And it seems to be working exactly as I wanted (yay!).

Could there be any pitfalls that I should be aware of?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜