Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I read somewhere about hot linking of images. Preventing image hot linking helps to stop bandwidth theft from your site. Would it work for a PHP file?

In my case, I am using a PHP file to generate thumbnails from an image. I don't want others to refer this PHP file from their site.

share|improve this question
    
This might be better suited on stackoverflow –  John Conde Oct 28 '10 at 4:26

1 Answer 1

up vote 4 down vote accepted

One programmatic way is to check the referrer to make sure the request came from your site:

<?php
    $yoursite = "yoursite.com"; //Your site url without http://
    $yoursite2 = "www.yoursite.com"; //Type your domain with www. this time

    $referer = $_SERVER['HTTP_REFERER'];

    //Check if browser sends referrer url or not
    if ($referer == "") { //If not, set referrer as your domain
        $domain = $yoursite;
    } else {
        $domain = parse_url($referer); //If yes, parse referrer
    }

    if($domain['host'] == $yoursite || $domain['host'] == $yoursite2) {

        //Run your image generation code

    } else {

        //The referrer is not your site, we redirect to your home page
        header("Location: http://yoursite.com");
        exit(); //Stop running the script

    }    
?> 

Source: http://www.knowledgesutra.com/forums/topic/40295-check-referrer-to-prevent-linking-yours-from-other-sites/

Edit
This article presents an alternate method using PHP sessions.

share|improve this answer
1  
Of course referrers can be spoofed quite easily. –  John Conde Oct 28 '10 at 4:28
    
Thanks, the same thing i found after posting the answer. lets check whether it'll work. just for knowledge, can i do it through htaccess file and keeping the file in folder where my php exist? –  articlestack Oct 28 '10 at 5:47
    
@articlestack: You can do referrer checking with htaccess but obviously not the 2nd solution since it uses PHP sessions. –  BenV Oct 28 '10 at 6:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.