Page 1 of 1

Counter should not Increment when Page Refresh HELP

Posted: Thu Jun 26, 2008 7:54 pm
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

Re: Counter should not Increment when Page Refresh HELP

Posted: Thu Jun 26, 2008 9:31 pm
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.

Re: Counter should not Increment when Page Refresh HELP

Posted: Thu Jun 26, 2008 10:28 pm
by Ninjataktikz
Zoxive. Thank You so much atleast this gave me a start. I was totally lost. Every bit helps. Thank you