Will perl set a value that the browser retains?

I am trying to track customer referal sales. I want a customer to hit a banner link on another site that sends him to my site and sets a CUSTID=somenumber variable value. Then, he can move around my site as he wishes.....if he decides to buy my product can i have him hit a link to a .cgi script that will place the CUSTID=somenumber variable in another form that posts to a script i have that reads the CUSTID=somenumber pair.
 Can this be done in perl, if so, how?
Who is Participating?
nchen091098Connect With a Mentor Commented:
There are 2 ways to do this.

The vast majority of the time, cookies are enabled on the client's browser, so you can set a cookie on their end.  To do this, have the banner call your script with a query string.  Like YOURSCRIPT.CGI?xxxxxx  where xxxxxx is the CUSTID #.

To set the cookie, put as your server response:

print "HTTP/1.0 200 OK\n";
print "Content-type: text/html\n";
print "Set-Cookie: CUSTID=$ENV{'QUERY_STRING'}\;\n\n

Then the id is stored on the clients browser.

To retrieve this cookie, use this standard function:

sub parse_client_cookies
      my ($key_value, $key, $value, $var_name, $var_value);
      my @key_value_pairs = split (/;\s/, $ENV{'HTTP_COOKIE'});
      foreach $key_value (@key_value_pairs){
            ($key, $value) = split (/\s*=\s*/, $key_value);
            $key =~ tr/+/ /;
            $value =~ tr/+/ /;
             $key =~ s/%([\da-fA-F][\da-fA-F])/pack("C", hex($1))/eg;
            $value =~ s/%([\da-fA-F][\da-fA-F])/pack("C", hex($1))/eg;
                  $value = pack "h*", ($value);
                  ($var_name, $var_value) = split (/\s*=\s*/, $value);
                  $cookie_var{$var_name} = $var_value;

the CUSTID is stored in the hash as $cookie_var{'CUSTID'}

The second way to do this is to store in a table the IP address of the client with the CUSTID, so you can look up the CUSTID when they make a purchase, but this takes more work.
you can also set an <input type=hidden> field,
or check $ENV{REMOTE_USER} after the client has responded to a
vernkAuthor Commented:
Thanx for the help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.