0

嗨,伙计们,我正在开发一个程序,该程序将执行访问网站的人数,当日期更改时,它将从 0 开始。所以我几乎弄清楚了如何做到这一点,但是当日期时它不会显示为 0这里的更改是我的代码:

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

1 回答 1

2

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.

于 2013-09-06T15:42:19.840 回答