Counter should not Increment when Page Refresh HELP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ninjataktikz
Forum Newbie
Posts: 5
Joined: Tue Jun 10, 2008 11:52 am

Counter should not Increment when Page Refresh HELP

Post by Ninjataktikz »

Hi guys,
I made a Hit Counter for my Hw assignment that basically counts everytime someone goes to the site. Here's the site just in case http://php.crc.losrios.edu/~w0957358/counter_a3.php . My assignment is basically done but for extra credit. We have to make it where the counter does not Increment when someone presses Refresh. It should only increase if someone visits newly fresh page and I do not have a single clue where I should start from. I am a total noob to this stuff. Please help if possible. Here is the code that I have typed.

Code: Select all

<?php
    $fp = fopen('counter.txt',"r");
    $num = fgets($fp);
    fclose($fp);
    print ("You have visited this page $num times.");
    $fp = $num++;
    $fp = fopen('counter.txt',"w");
    fwrite($fp,$num);
    fclose($fp);
?>
Thank You
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Counter should not Increment when Page Refresh HELP

Post by Zoxive »

The only things I can think of right away, are using $_SESSION, or just setting a cookie. And if that cookie exists then don't increment.

set_cookie();

Sessions

Code: Select all

 
if(empty($_COOKIE['mycookiename'])){
 // Increment, and set cookie
}else{
// Already visited page so we still want to show the counter, just dont increment it
}|
 
// OR Sessions
 
session_start();
if(empty($_SESSION['myvarname'])){
  // Increment
  $_SESSION['myvarname'] = true;
}else{
  // Already visited page so we still want to show the counter, just dont increment it
}
 
Thanks probably as much as i can show you with out doing it all for you.. It is your assignment after all.
Ninjataktikz
Forum Newbie
Posts: 5
Joined: Tue Jun 10, 2008 11:52 am

Re: Counter should not Increment when Page Refresh HELP

Post by Ninjataktikz »

Zoxive. Thank You so much atleast this gave me a start. I was totally lost. Every bit helps. Thank you
Post Reply