funz has asked for the wisdom of the Perl Monks concerning the following question:

Ive been trying to figure it out all day today and yesterday. I am trying to create a script that you link to from an image using the standard image tag, Example: <img src="http://www.domain.com/script.cgi"> or even <img src="http://www.domain.com/script.cgi?image">

The problem is that everytime its called it stores the page where the image is being called from, instead of the referrer to the page. Example: I link the site from http://www.site.com and go from http://www.site.com to http://www.domain.com, so it should store http://www.site.com as a referrer but instead it stores http://www.domain.com. This is the main program im using, but ive modified it a couple of times, calling it form SSI, which works, but not from an image.

#!/usr/bin/perl $store = "/home/domain/public_html/tracker/data.dat"; open (DATAF,">>$store"); print DATAF "$ENV{'HTTP_REFERER'}\n"; close(DATAF); print "Content-type: image/gif\n\n"; open(IMG, "/home/domain/public_html/image.gif"); print <IMG>; close(IMG); exit;

Replies are listed 'Best First'.
Re: $ENV{'HTTP_REFERRER'} and images
by shemp (Deacon) on Jun 10, 2004 at 21:13 UTC
    the HTTP_REFERRER contains the URL of the page that the user had on their browser when the current request is made. So, when you are on the page www.site.com and click the link to go to www.domain.com, at that point www.site.com is the HTTP_REFERRER.
    Then you're looking at the page at www.domain.com, and you click the image. So www.domain.com is the HTTP_REFERRER. If you need the referrers referrer, you'll need to pass that info along when www.domain.com is served up.

    cookie, cgi hidden field, i dont know exactly what you're doing, but there should be some storage mechanism that suits your needs.
Re: $ENV{'HTTP_REFERRER'} and images
by hsinclai (Deacon) on Jun 10, 2004 at 21:14 UTC
    If I read your question correctly, everything is working as it should be...

    http://www.domain.com has referer http://www.site.com

    The image is loaded from a page (script) at http://www.domain.com/ - so that's the image's referer.



      Yes that is what happends but I want the referrer to be the http://www.site.com, not the http://www.domain.com which is where the script is called from. Basically this is all thats left for me to finnish a access logging script. Which will store the referrers to a page using an image tag.
        I think that the thing you're missing here is that you cant create your own definition of HTTP_REFERRER and expect it to work.

        As i said earlier, when you serve up the page at www.domain.com, you'll have to store its referrer somewhere to access later and use in the script that runs when the image is clicked.
        HTTP_REFERRER is a client side constructed entity, IIRC. Not all browsers send it, and none are required to as I understand. It is trivially spoofed, and shouldn't be counted on too heavily.

        Why don't you let the image reside (and be fetched from) the server you want?



        Your webserver logs (if properly configured) will give you all the information you need (al the warnings about fake-referer headers still apply of course). With some scripting you'll even find the grandparent-referers if thats what you are after.

        For instance if you look at this apache access log (I've modified the content, so might contain typo's):

        1.1.1.1 - - [10/Jun/2004:14:50:41 -0700] "GET /index.html HTTP/1.1" 20 +0 808 "http://www.othersite.com/" "Mozilla/4.0 (compatible; MSIE 6.0; + Windows NT 5.1; FunWebProducts)" 1.1.1.1 - - [10/Jun/2004:14:50:51 -0700] "GET /index2.html HTTP/1.1" 2 +00 1200 "http://www.mysite.com/index.html" "Mozilla/4.0 (compatible; +MSIE 6.0; Windows NT 5.1; FunWebProducts)" 1.1.1.1 - - [10/Jun/2004:14:50:51 -0700] "GET /logo.png HTTP/1.1" 200 +15000 "http://www.mysite.com/index2.html" "Mozilla/4.0 (compatible; M +SIE 6.0; Windows NT 5.1; FunWebProducts)"
        You can see somebody came from www.othersite.com to www.mysite.com/index.html and after 10 seconds he loaded index2.html which contains logo.png. If you can't get this information from the logging provided I suggest you read the Apache documentation on their logging formats.

        Why do you need a picture-referer-thingie for that?
Re: $ENV{'HTTP_REFERRER'} and images
by etcshadow (Priest) on Jun 10, 2004 at 23:13 UTC
    A couple things.
    1. All information the browser sends you is potentially suspect, so keep that in mind.
    2. HTTP_REFERRER for an <img> is the page on which the <img> tag sits. No amount of wishing is going to alter that.
    3. Your best bet is to embed in the img src, a query string parameter reflecting what information you want to pass along. Something like:
      print "<img src=http://www.site.com/my_image.gif?GRANDPARENT=".escape_ +uri($ENV{HTTP_REFERRER}).">";
      So that the HTML generated ends up looking like: <img src=http://www.site.com/my_image.gif?GRANDPARENT=http%3a%2f%2fwww.domain.com%2fmy_other_page.pl">. Then, you can extract that information from the query string when serving the image (or from the access logs on that server, or whatever).
    ------------ :Wq Not an editor command: Wq
      Thanks for the help guys, what I ended up doing is using javascript to get the referrer and the when then the program gets the referrer that was added to the query string in the image.