increment variable each time page gets called or opened?

后端 未结 7 1117
盖世英雄少女心
盖世英雄少女心 2021-01-28 17:57



Index

Question
相关标签:
7条回答
  • 2021-01-28 18:48

    No, because $q resets to 1 each time the page is called. You need some sort of persistence strategy (database, writing to a text file, etc) in order to keep track of the page views.

    It's also a good idea to consolidate this functionality into a class, which can be used across your code base. For example:

    class VisiterCounter {
    
        public static function incrementPageVisits($page){
    
            /*! 
             * Beyond the scope of this question, but would
             * probably involve updating a DB table or
             * writing to a text file
             */
            echo "incrementing count for ", $page;
        }
    
    }
    

    And then in newquestion.php,

    VisiterCounter::incrementPageVisits('newquestion.php');
    

    Or, if you had a front controller that handled all of the requests in your web application:

    VisiterCounter::incrementPageVisits($_SERVER['REQUEST_URI']);
    
    0 讨论(0)
提交回复
热议问题