How to stop access of a PHP file from other sites?

Asked  Aug 05, 06:28 pm 35 Views Add Your Answer
Asked  Aug 05, 06:28 pm

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.

0 Dislikes

1 Answers

Added  Aug 05, 06:28 pm

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

    $yoursite = ""; //Your site url without http://
    $yoursite2 = ""; //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
        exit(); //Stop running the script


0 Likes 0 Dislikes
Google login
SignUp with Email