yzxben
yzxben

Reputation: 872

Detect if an image is loaded in a page outside my domain

I would like to create a single URL that returns one image when loaded in a page within my domain, and another slightly modified image when loaded in a page outside my domain.

I am thinking along the lines of something like:

<?php
    header('Content-type: image/jpg');
    if (/**image is loaded within my domain**/)
    {
        readfile("image1.jpg");
    }
    else
    {
        readfile("image2.jpg");
    }
?>

Is there something I can put in the if-statement to make it work? Possibly that works in all browsers?

Is there a way to do this without php?

Upvotes: 1

Views: 146

Answers (2)

kirilloid
kirilloid

Reputation: 14304

I don't know, how you're going to get request for image not in your domain, but you may look at $_SERVER['HTTP_HOST']. Maybe, HTTP_REFERER is what you need. Anyway look here.

Upvotes: 1

F21
F21

Reputation: 33401

You could use the referrer URL in the request and check to see if it is your domain. This is done using $_SERVER['HTTP_REFERER'].

However, the HTTP_REFERER URL can be easily modified by clients and can even sometimes not be set, so you need to be careful when using it.

Upvotes: 3

Related Questions