Doolkin
Doolkin

Reputation: 119

PHP - consecutive page visits

my question is simple : How can I count how many consecutive days visitor have visited my site (php), any ideas are welcome.

Upvotes: 2

Views: 370

Answers (3)

Evan Teran
Evan Teran

Reputation: 90442

Simple:

Just have some concept of either logging in, or a persistent cookie (logging in is more reliable since they can clear cookies). Then in your database have a field for "last logged in". If the last logged in field matches yesterday's date, increment your consecutive visit count, otherwise reset it.

EDIT: it's probably obvious, but make sure you update the "last logged in" field after you check it to be today, otherwise every time they load the page it'll increment the count!

EDIT: a quick example may look something like this (psuedo code):

// first you need to set $last seen from the DB..
// first you need to set consecutive from the DB too..
// once you have, you can do something like this.
if(strtotime('-1 day', date('Y-m-d')) == $last_seen) {
    $consecutive = $consecutive + 1;
    mysql_query(sprintf('UPDATE user SET last_seen=NOW(),consecutive=%d WHERE id=%d', $consecutive + 1, $id)); 
} else if(date('Y-m-d') == $last_seen) {
    // ok, they logged in today again, do nothing.
} else {
    $consecutive = 0; // only really needed if you plan on displaying it to the user
                      // later in this script
    mysql_query(sprintf('UPDATE user SET last_seen=NOW(),consecutive=0 WHERE id=%d',$id)); 
}

Upvotes: 6

user334596
user334596

Reputation:

You can use pluggable traffic analitic tools, commercial or less like Google Analitics.

Upvotes: 1

Fire Crow
Fire Crow

Reputation: 7729

cookies

if(isset($_COOKIE["identifier"])){
   // log as existing user
}else{
  setcookie('identifier', $id_value, ...
}

It wont work if people clear there cookies, which is why they clear cookies.

Upvotes: 1

Related Questions