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

There may or may not be something wrong with the code. I've heard that it's not always possible to determine which site a user came from before they found yours.

I've tried to write a script that records the referring URL but it keeps up coming Unknown. I automatically set blank $ENV{HTTP_REFERER}s to Unknown just so that data isn't wasted.

Since I've installed the script, I've had 53 vistors, ALL have showed up as Unknown. Just to test things out I added a link to my website on my homenode, but that came back as Unknown as well.

Did I goof something up in the script below?

my %list; my $list = "refer.dbm"; my $file = "refer.txt"; print header, start_html; tie %list, 'SDBM_File', $list, O_CREAT | O_RDWR, 0644; if ( !tied %list ) { print "database unsuccessful $!.\n"; } my $ip = $ENV{REMOTE_ADDR}; my $location = $ENV{HTTP_REFERER}; if ( exists $list{$location} ) { my $value = $list{$location}; $value++; $list{$location} = $value; } elsif ( $list{$location} eq "" ) { if ( exists $list{Unknown} ) { my $value = $list{Unknown}; $value++; $list{Unknown} = $value; } else { $list{Unknown} = "1"; } } else { $list{$location} = "1"; }


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: $ENV{HTTP_REFERER}; not showing up
by Gerard (Pilgrim) on Dec 09, 2003 at 03:57 UTC
    This question has already been asked several times and answered I should think.These may provide the answer to your questions.

    Regards,
    Gerard
Re: $ENV{HTTP_REFERER}; not showing up
by davido (Cardinal) on Dec 09, 2003 at 05:23 UTC
    I see nothing syntactically wrong with your capturing of $ENV{HTTP_REFERER} into $location. Just to be safe, try outputting the contents of $ENV{HTTP_REFERER} to a logfile of some sort early on in your script to ensure that it is the same as the value you are working with in $location later in the script. ...never hurts to check that sort of thing.

    But as others have mentioned, never rely entirely on the $ENV{HTTP_REFERER} value being set to what you think it should be. Browsers may not provide the info, or proxies may strip it away. In fact, a script acting as a browser could even spoof the HTTP_REFERER value. It's simply not reliable.


    Dave

Re: $ENV{HTTP_REFERER}; not showing up
by Roger (Parson) on Dec 09, 2003 at 05:39 UTC
    Was that you who posted this node 310630 before as Anonymous Monk? Looks like you haven't digested anything yet from that previous post.

    You still have the ugly if (exists...) ... block untouched. Won't it be better if you just replace the entire block of code with -
    $list{ $location || 'Unknown' }++;


    And I second davido's suggestion above on never to trust/rely completely the value set in HTTP_REFERER. It could even be faked.

      I just looked at that node you provided, I must say it looks like my code :) I rarely ever post as anonymous and I don't remember making it, but it was probably mine.

      I know this isn't going to always work and if it does, the data won't be reliable. This is just to be a fun little script to try to make an attempt to figure out where my traffic is coming from.

      I am almost possitive there is a logic error, like someone said above. I'll take a quicker glance to see if I can figure it out, thanks for your help everyone!



      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
Re: $ENV{HTTP_REFERER}; not showing up
by edoc (Chaplain) on Dec 09, 2003 at 05:15 UTC

    Is there a logic error in there?

    if ( exists $list{$location} ) { my $value = $list{$location}; $value++; $list{$location} = $value; # if $list{$location} doesn't exist then it always eq '' ? # } elsif ( $list{$location} eq "" ) { # test $location instead? } elsif ( $location eq "" ) { if ( exists $list{Unknown} ) { my $value = $list{Unknown}; $value++; $list{Unknown} = $value; } else { $list{Unknown} = "1"; } } else { $list{$location} = "1"; }

    cheers,

    J