Use MySQL in PHP to access statistics ideas and implementation code

xiaoxiao2021-03-05  22

I have read a lot of statistics on the Internet to store information, but that is very inconvenient, and if the data is lost, it is very troublesome. If you are stored in the database, it is better, and the web server and database server are separated. Then you can keep your access statistics for a long time.

Basic access statistics include: daily visits, monthly visits, total visits, average access, day maximum visits, etc.

Those data is mainly about time operations. For convenience of operation, we use UNIX timestamps to be the most convenient and reasonable. Here I simply say the following I implements statistics.

Database structure:

## Visit Statistics Table #

DROP TABLE IF EXISTS `st_accesscount`; CREATE TABLE` st_accesscount` ( `access_id` int (11) unsigned NOT NULL auto_increment,` session_id` varchar (100) NOT NULL default '', `access_time` int (20) NOT NULL default ' 0 ', `access_ip` varchar (100) default NULL,` access_source` varchar (100) default NULL, `access_page` varchar (255) default NULL,` access_os` varchar (100) default NULL, `access_browse` varchar (100) Default Null, Primary Key (`Access_ID`);

Implement code:

I wrote the core code, other display layer code and complex calculations I think, huh, huh :-)

/ * Website access statistics * / / * Basic variable * /

// Current time $ cur_date = Date ("Y year M month D day h: i: s"); $ TIME = TIME ();

/ / Take the current time separately $ C [Y] = Date ("y"); $ c [m] = DATE ("m"); $ C [D] = DATE ("D"); $ C [h ] = DATE ("H"); $ C [i] = Date ("i"); $ c [s] = DATE ("s"); // Today's visit $ Today = mktime (0,0,0 $ DB-> Query ("SELECT * from ST_ACCESS WHERE Access_time> = $ TODAY and Access_time <= $ TIME"); $ TODAY_COUNT = $ dB-> nf ();

// Yesterday's visits $ yesterday = mktime (0, 0, $ c [m], $ C [d] -1, $ c [y]); $ db-> query ("SELECT * from ST_ACCESSCOUNT WHERE Access_time > = $ yesterday and access_time <= $ {"); $ yesterday_count = $ db-> nf (); // This month's visits $ month = mktime (0, 0, 0, $ c [m], 1, $ C [y]); $ db-> query ("SELECT * from str_accesscount where access_time> = $ month and access_time <= $ time"); $ month_count = $ db-> nf ();

// Presidential counts $ db-> query ("SELECT MIN (Access_Time) AS AGO_TIME from St_AccessCount"); while ($ db-> next_record ()) {$ AGO_TIME = $ db-> f ("AGO_TIME");} $ Day = ($ TIME- $ AGO_TIME) / 60/60/24; ($ Day-Floor ($ Day))> 0)? ($ all_day = floor 1): ($ all_day = floor ($ day));

// Total visual amount $ db-> query ("select * from st_accesscount"); $ all_count = $ db-> nf (); // average average access ($ all_count / $ all_day, 1);

?>

The above code is clearer, slowly pondering, wing, and other words, the PHPLIB's DB class library is used. . .

转载请注明原文地址:https://www.9cbs.com/read-38401.html

New Post(0)