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

A search on Google led me to the idea that adding cookie headers to an image would set the cookie as the image loaded. What I would eventually like to do is pick up HTTP_REFERER and create a conditional so that it is not overwritten when the user returns to the page from within the site.

The following sets the cookie when img.pl is called directly, but not when it loads as part of the HTML page (using <img src="img.pl">). The image loads so the script is obviously being run.

Is there something else that needs to be done, or am I barking up the wrong tree?

#!/usr/bin/perl -w use strict; use CGI ':standard'; my $pageref = 'hello'; # Refresh the cookie so that it doesn't expire. my $perl_cookie = cookie(-name=>'referimg', -value=>$pageref, -expires=>'+30d'); print header(-cookie=>$perl_cookie); my $file = "images/logonavy.gif"; open FILE, "$file" or die $!; while (<FILE>) { print $_; }

Replies are listed 'Best First'.
Re: setting a cookie via an image
by tachyon (Chancellor) on Aug 28, 2004 at 10:46 UTC

    You are forgetting the Content-Type: image/gif header. I don't know if that is relevant. binmode FILE is also a good idea, though not relevant. It may simply be that the browser is dropping the headers when img is called from a page.

    It rather begs the question - why aren't you just setting the cookie when the main page loads. This sort of stuff usually makes me think spyware.

    cheers

    tachyon

      In the original question I stated that I needed a conditional to check if the information is already there - otherwise reloading the page would overwrite the original information. there is nothing sinister about it at all.

      Why do I need the information? A lot of users to that page are referred and I need to keep track of where they came from. But if they take a look around the site and reload that page then the original information is lost as a new cookie is set

      I added the header but it didn't make a difference - thanks anyway

        It sounds like you simply have a broken implementation of intrasite user tracking. The usual logic is:

        if ! tracking_cookie make new session ID log referer, browser, blah as initial data add entry page to track log for session ID send cookie with session ID when returning page else add page to track log for session ID

        Once you have set a cookie (with a session ID) reloading a page will still send that cookie to you provided the path is correct.

        cheers

        tachyon

Re: setting a cookie via an image
by PodMaster (Abbot) on Aug 28, 2004 at 10:56 UTC
    So your question is why its not setting the cookie? It could be a million reasons, but mostly because the browser doesn't like the cookie information you're feeding it. Maybe it considers it a 3rd party cookie, who knows. What you want to do is specify every part of the cookie (domain, path ... refer to docs).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

HTTP_REFERER (was RE: setting a cookie via an image)
by Mr. Muskrat (Canon) on Aug 28, 2004 at 14:08 UTC

    HTTP_REFERER is not guaranteed to be accurate. Some firewalls strip that information out before allowing the page request to leave the network. Some browsers strip or modify it as well. Way too many people know how to fake it too.