Archive for the tag 'php code'

seth

Counter Class

Counter Class

Counter class can be used to keep a count and timer of a given object or group of objects. I use it mainly for counting links and stopping and starting a timer in session. This helps me to keep track of how long a user is on a particular page or group of pages. You may find it useful to call this object through AJAX to update counter information in your database, or just hit it using a normal post. In my case, I wrote a small extension of this class which does all of the necessary DB handshaking between the counter data and the database.

Usage:
* @example $counter = new Counter(100) - this instantiates the $counter object and sets the initial counter 'page'=>100,'count'=>1
* @example $counter = new Counter(null,true); - this instantiates the $counter object and sets the initial counter [page]=>'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'],[count] => 1, [time] => 0
If the second value is true, the clock will start ticking on your page's session variable. Every time the counter is called again, the session variable will update to the time difference between the second call and the initial call. You may use the other methods such as, stopTimer, restartTimer to do the obvious. When finished counting and timing, you may obtain all of your counts and times using the getAllCounts method. That's about it. It's simple, but it comes in real handy for me when attempting to, well, count things.

Counter Class