Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...This site is like first coffee in the winter morning..."

Geography

Where in the world do Tek-Tips members come from?
DEAG (Programmer)
19 Apr 11 13:10
Hello everyone,

I'm having trouble getting this script working.

Current issue:
When running the script, I get "You have visited this page 1 time(s)." then when I refresh the page it continue showing "1" instead of incrementing up to "2".

Desired output:
When Launching the script a cookie is created and the counter start at 1. When the page is refreshed or visited again, the number increment up to 2, 3 and so on.

Can someone please help me out with this? Let me know if you need more info...

Here's my script:

#!/usr/bin/perl
print "Content-type: text/html\n\n";
use CGI qw(:standard -debug);
use strict;
use CGI::Carp qw(fatalsToBrowser);
use warnings;

#declare variables
my ($C_visits, $counter, %counter);

#Read the cookie to a variable
%counter = (cookie('$counter'))? cookie('$counter'):'none';

#create cookie
$C_visits = cookie(-name => "MyCookie",
                    -value => "$counter",
                    -expires => "+3M");


if (exists $counter{$_})    {
    $counter{$_}++;
} else    {
    $counter{$_} = 1;
}

#send cookie to browser
# print header(-cookie => $counter);

#create Web page
print "<HTML>\n";
print "<HEAD><TITLE>My Counter</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H1 ALIGN=left>Counter<BR></H1><HR>\n";
print "<ALIGN=left>You have visited this page $counter{$_} time\(s\).\n";
print "</BODY></HTML>\n";
MillerH (Programmer)
29 Apr 11 2:43
The cookie needs to be output as part of the header.  You currently print out the content type at the start of your script which means that everything after that is part of the html body:

The following will work for you:

CODE

#!/usr/bin/perl -wT

use CGI qw(:standard -debug);
use CGI::Carp qw(fatalsToBrowser);

use strict;
use warnings;

# Read the cookie to a variable
my $counter = cookie('counter') || 0;
$counter++;

# create cookie
my $cookie = cookie(
    -name => "counter",
    -value => $counter,
    -expires => "+3M"
);

# Header with cookie
print header(-cookie => $cookie);

# create Web page
print <<"END_HTML";
<HTML>
<HEAD><TITLE>My Counter</TITLE></HEAD>
<BODY>
<H1 ALIGN=left>Counter<BR></H1><HR>
<ALIGN=left>You have visited this page $counter time(s).
</BODY></HTML>
END_HTML

1;

__END__

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close