For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > December 2004 > Setting a Cookie...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Setting a Cookie...
Bill Stephenson

2004-12-08, 3:55 pm

How can I set a cookie when someone visits my home page?

I've tried using a server side include like so....

######################### code #########################
#!/usr/bin/perl -w

# This is in my html page:
# <!--#exec cgi="/cgi-bin/ezInvoice2/ssi.cgi"-->

use strict;
use CGI qw/:standard/;
use CGI::Carp('fatalsToBrowser');

my $Q = new CGI;
my $visits;

$visits = $Q->cookie('visit_tracker');

if ($visits eq '') {
$visits = 1;
}

else {$visits++}

my $visit_tracker= $Q->cookie(-name=>'visit_tracker',
-value=>"1",
-expires=>'+10y');

print $Q->header(-cookie=>$visit_tracker);
print "You've been here $visits times";
####################### end code #######################

This inserts "You've been here 1 times", but does not set the cookie.
Nor does it create any errors in my server logs.

I assume this can be done with some "Apache" programming, but I know
almost nothing about that and I'd rather do this with perl if possible.
Any tips will be much appreciated...

Kindest Regards,

Bill Stephenson

Bill Stephenson

2004-12-08, 3:55 pm

I apologize, the code I sent previously does create an error, this code
does not...

######################### code

#!/usr/bin/perl -w

use strict;
use CGI qw/:standard/;
use CGI::Carp('fatalsToBrowser');

my $Q = new CGI;

my $demo_uses;

$demo_uses = $Q->cookie('demo_tracker');

if (!$demo_uses) {

$demo_uses = 1;

}

else {$demo_uses++}

my $demo_tracker= $Q->cookie(-name=>'demo_tracker',
-value=>"$demo_uses",
-expires=>'+10y');


print $Q->header(-cookie=>$demo_tracker);

print "Here's Something Big!";

######################### end code


Kindest Regards,

Bill Stephenson

Bob Showalter

2004-12-08, 3:55 pm

Bill Stephenson wrote:
> How can I set a cookie when someone visits my home page?
>
> I've tried using a server side include like so....
>
> ######################### code #########################
> #!/usr/bin/perl -w
>
> # This is in my html page:
> # <!--#exec cgi="/cgi-bin/ezInvoice2/ssi.cgi"-->


[snip cgi ]

Your code to set the cookie is OK, but I don't think you can do this from
SSI; only the body of the response is used, not the headers (where the
cookie is set).

Googling for "set cookie from ssi" turns up lots of discussion of this
topic.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com