Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • 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!

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

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

CGI counter

CGI counter

CGI counter

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:

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{$_})    {
} 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";

RE: CGI counter

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:


#!/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;

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

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

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



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

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! Already a Member? Login

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