rocker789
rocker789

Reputation: 259

Destroy session in PHP

Hi guys I am working on a program which will execute the number of people visit a website and when the date changes it will start from 0. So I have nearly figure out how to do it but it doesn't appear as 0 when the date changes here is my code:

<?php
    session_start();
?>
            <?php
            if(isset($_SESSION['views']))
                $_SESSION['views']=$_SESSION['views']+1;
            else
                $_SESSION['views']=1;
                echo "You are the ". $_SESSION['views'] ." Visitor";
        ?>

Upvotes: 0

Views: 101

Answers (1)

xlecoustillier
xlecoustillier

Reputation: 16351

As @Zwirbelbart said, don't use sessions for solving this. Use a DB, or at least a file, where you'll store the number of visitors.

Something like this:

function incrementVisitorsCount() {
    $currentDay=date("Ymd");
    if(!isset$_SESSION["visited"] || $_SESSION["visited"] != $currentDay) {
        incrementYourDailyCounter($currentDay);
        $_SESSION["visited"]=$currentDay;
    }
}

incrementYourDailyCounter being the function that will increment the relevant value in the storage you chose (I would suggest a table in a DB you're most certainly already using).

You can base your counter on IP instead of sessions, but it means that you keep a record of each IP that visited your website each day.

Upvotes: 2

Related Questions