marcnyc marcnyc - 6 months ago 9
PHP Question

how to increment a counter only if it is a new session?

I have php counter script that simply reads the visitor number from a text file, increments it by 1 and writes the new value to the same text file.
This of course counts every page refresh but I would like to only count unique visitors, and not count page refreshes. How can I do that?
I tried starting a session at the beginning of my main file and then checking whether the session exists but because the session always exists when the file is loaded the counter never gets incremented... I am probably missing something stupid... can somebody point me in the right direction? thanks

Answer

You may set a flag, if it doesnot exists already, then you do some action:

session_start();

if(!isset($_SESSION['started'])){
    $_SESSION['started'] = true;
    //Increment your counter here
}