: This article mainly introduces how PHP collects and displays website traffic based on cookies and Sessions. For more information about PHP tutorials, see. This example describes how PHP collects website traffic statistics based on cookies and sessions and outputs the display results. We will share this with you for your reference. The details are as follows:
<? Php $ f_open = fopen ("count.txt", "r +"); // open the specified file $ count = fgets ($ f_open ); // read the data in the file if (empty ($ _ COOKIE ['cookie _ name']) {// Determine whether the cookie exists setcookie ("cookie_name", value, time () + 1800); // If not, create COOKIE $ count = $ count + 1; // add the value of variable $ count to 1 rewind ($ f_open ); // open the specified file fwrite ($ f_open, $ count); // write new data to the file fclose ($ f_open); // close the file}?>
<? Php session_start (); include ("conn/conn. php "); $ data1 = date (" Y-m-d "); // Obtain the current access time $ data2 = substr (date (" Y-m-d "), 0, 7); $ ip = getenv ('remote _ ADDR '); if ($ _ SESSION [temp] = "" | $ _ SESSION [temp] = NULL) {// Determine whether the value of $ _ SESSION [temp] = "" is null, where temp is a custom variable // use the database to store data $ select = mysql_query ("select * from tb_count10 where data1 = '$ data1' and ip = '$ IP '"); if (mysql_num_rows ($ select)> 0) {$ query1 = "update tb_count10 set counts = c Ounts + 1 where data1 = '$ data1' and ip = '$ IP' "; $ result1 = mysql_query ($ query1 );} else {$ query = "insert into tb_count10 (counts, data1, data2, ip) values ('1', '$ data1', '$ data2', '$ IP ') "; $ result = mysql_query ($ query) ;}$ _ SESSION [temp] = 1; // after logon, the value of $ _ SESSION [temp] is not empty, assign $ _ SESSION [temp] a value of 1}?>
<? Php // output the number of records in the database in graphical form $ query = "select sum (counts) as counts from tb_count04 "; // query the total access volume in the database $ result = mysql_query ($ query); $ visitor = mysql_result ($ result, 0, 'counts'); echo "----------"; echo"Website Access volume:"; // Display the number of visits in a graphic manner // process the value 0 of the complement digit $ len = strlen ($ visitor ); // Obtain the string length $ str = str_repeat ("0", 6-$ len); // Obtain 6-$ len digits 0 for ($ I = 0; $ I