Gossamer Forum
Home : General : Perl Programming :

Need some advise!?

Quote Reply
Need some advise!?
I’m now already trying to solve the following problem for a while. I tried different solutions, but can’t seem to find a working one Frown

I’m trying to do……

On my website I have some forms for the visitor to fill-in. On these form I asked the visitor, how he or she found us. But I found out that not a lot of people answer the question accurate. So I would like to set a cookie with the referer on the page a visitor enters my website (usually http://www.domain.com/index.html) and read the cookie when the form (a couple of pages further.....) is filled in. The reading parts works fine. It’s getting the referer and putting it into a cookie.

What did I try………..

A lot. First I made a script that set’s a cookie based on the HTTP_REFERER and displays an image (1x1 pixel). It works if you surf directly to the script, but included as an image-tag in index.html the referer doesn’t seem to pass. Probably, but I’m not 100% sure this is because the referer isn’t forwarded to the image tag. (?!) I alsow tried to make a script working as an javascript sourse file, but did’t work for the same reason.

So I thought: get the referer with javascript and print an image-tage with the document.write statement. For this I made sure that the script sets the cookie based on the param r [$referer = $in->param('r');]. The good thing is that if surf directly to the script (and manually give a r=) it works, only again it doesn't seem to work in combination with the javascript I have.

My question………….

Before I upload all the codes and snippets I tried I would like to ask you guys, what would be the best way of solving this problem. Which technologies or way of working. Then I can try to make i happen myself!

Note: (Just to avoid comments about the cookie setting Wink)

I don set the complete referer as the cookie value, but cleansed and crypted. For this I use the following two lines of code. Thanks to Paul for the code. Many thanks acually!

my $referer = ‘http://www.foo.bar/pagename’; my $crypt;
$referer =~ s|^http://([^/]+).*|$1|i;
$crypt = join ("-", map { ord } (split //, $value));


Last edited by:

cK: Jun 1, 2002, 3:32 AM
Quote Reply
Re: [cK] Need some advise!? In reply to
I guess you could use SSI to include a hidden field in the form.


#!/usr/bin/perl

referer code here

print a header
print qq|<input type="hidden" name="referer" value="$value">|;
Quote Reply
Re: [Paul] Need some advise!? In reply to
Oeps, I wasn't clear enough. (Will go though the text again.)

I want to SET the cookie (with the external referer) on my index.html page. Retrieving the cookie (a couple of pages later.....) isn't a problem, I just added a snippet to my existing script.

For example: somebody I somebody finds my website on google.com:

google.com/search.cgi?keyword=foo

1) http://www.domain.com/index.html ( here google.com should be set as cookie)

2) http://www.domain.com/page1.html

3) http://www.domain.com/page2.html

4) http://www.domain.com/page3.htm

5) http://www.domain.com/form.html (here the cookie is retrieved, works fine!)

It's al about setting a cookie on index.html Wink

Thanks for all help in advance!
Quote Reply
Re: [cK] Need some advise!? In reply to
How about using javascript?

Code:
<script language="javascript">
function cookie() {
document.cookie = "referer=" + document.referer;
}
</script>

Or something like that :)
Quote Reply
Re: [Paul] Need some advise!? In reply to
As is wrote I tried that but it didn't work.

That's why I would like to figure our what SHOULD work before I go on with trying.

Included the following javascript on all entry pages:

Code:

<script language="JavaScript" src="/js/ref.js"></script>

ref.js was:

Code:

var ref = parent.window.document.referrer;
var tag = 'http://www.domain.com/cgi-bin/track.cgi?ref=' + ref + '&lang=NL';
document.write('<IMG BORDER="0" WIDTH="1" HEIGHT="1" SRC="'+ tag +'">');


track.cgi was:

Code:

my $in = new CGI;
my $referer = $ENV{'HTTP_REFERER'};


$referer =~ s|^http://([^/]+).*|$1|i;
my $crypt = join ("-", map { ord } (split //, $referer));
if (length($crypt) < 5) { $crypt = ""; }


my $cookie = $in->cookie(
-name => 'id',
-value => $crypt,
-domain => '',
-expires => '+16h',
-path => '/'
);


print $crypt ? $in->header( -type => 'image/gif', -expires => '+1h', -cookie => $cookie ) : $in->header( -type => 'image/gif', -expires => '+1h' );

open (IMG, "/home/html/site1/images/pixel.gif") or die $!;
while (<IMG>) { print; }
close IMG;


__END__

Last edited by:

cK: Jun 1, 2002, 3:55 AM
Quote Reply
Re: [cK] Need some advise!? In reply to
For some strange reason I cannot edit al my own posts, therefore this reply. (This one I can!! Strange!?)

All the cookie-setting scripts work fine, I only cannot seem to get the referer at track.cgi. Even the following hard-coded line doesn't seem to work! Why? (It seems like it doesn't set the cookie until I refresh my index.html.)

Code:
<img src="/cgi-bin/track.cgi?ref=http://www.demo.com/" width=1 height=1 border=0 alt="">

..the next step would be some javascript to get the real referer and not www.demo.comWink

Last edited by:

cK: Jun 1, 2002, 9:55 AM