This article to the big this introduces PHP to write some of the commonly used Web site statistics code, with no database and the use of database and HTML static page views of the statistics code, we can enter the reference.
Example 1
Code for statistics directly using TXT files
<?PHPSession_Start();//define session, the same IP login does not accumulate $filepath= ' Count.txt ';//count.txt Number of statistics if($_session[' temp '] = = ') {//determines whether the value of $_session[temp] is empty, where temp is a custom variable if(!file_exists($filepath)){ //checks if the file exists, does not exist, and assigns a value of 0 to the newly created file $fp=fopen($filepath, ' W '); fwrite($fp, 0); fclose($fp); Counter ($filepath); }Else{counter ($filepath); } $_session[' temp '] = 1;//after login, assign a value of 1 to $_session[temp] }Echo' Welcome to the lazy material website, you are the site <font color= "#FF0000" > ".file_get_contents($filepath).‘ </font> Visitors ';//the Counter () method is used to get the numbers in the filefunctionCounter$f _value){ //when you open a file in W mode, the contents are emptied, so open it in R mode, take out the contents of the file, save to the variable $fp=fopen($f _value, ' R ') or die(' An error occurred while opening the file. ‘); $countNum=fgets($fp, 1024); fclose($fp); $countNum++; $FPW=fopen($f _value, ' W '); fwrite($FPW,$countNum); fclose($FPW);} //Note the following line can achieve the same IP login not cumulative effect, the test can be opened//Session_destroy ();?>
The above is using a TXT file, let us introduce a MySQL database operation example
CREATE TABLE' MyCounter ' (' ID ')int( One) not NULLauto_increment, ' Counter 'int( One) not NULL, ' Counterlastday 'int(Ten)default NULL, ' Countertoday 'int(Ten)default NULL, ' recorddate ' date not NULL, PRIMARY KEY(' id ')) ENGINE=InnoDBDEFAULTCHARSET=GBK auto_increment=2;
<?PHPSession_Start(); if( !isset($_cookie["User"]) ){ Setcookie("User", "newguest", Time() +3600); }Else { Setcookie("User", "oldguest"); } functionShowmycounter () {$id=$_get[' ID ']; //Defining Variables $IsGone=FALSE; //connecting to a database $conn=mysql_connect(' localhost ', ' root ', ' root '); mysql_select_db(' Php_test ',$conn); //reading Data $querysql= "SELECT * from ' mycounter ' WHERE id = '$id‘ "; //$querysql = "select * from ' mycounter '"; $queryset=mysql_query($querysql,$conn); $row=Mysql_fetch_array($queryset); //get the amount of time $DateNow=Date(' y-m-d '); $RecordDate=$row[' Recorddate ']; $DateNow _explode=Explode("-",$DateNow); $RecordDate _explode=Explode("-",$RecordDate); //Judging whether the day has passed if($DateNow _explode[0] >$RecordDate _explode[0]) $IsGone=TRUE; Else if($DateNow _explode[0] = =$RecordDate _explode[0] ) { if($DateNow _explode[1] >$RecordDate _explode[1] ) $IsGone=TRUE; Else if($DateNow _explode[1] = =$RecordDate _explode[1] ) { if($DateNow _explode[2] >$RecordDate _explode[2])$IsGone=TRUE; } Else Break; } Else Break; //according to the Isgone to do the corresponding operation IF($IsGone) { $RecordDate=$DateNow; $CounterToday= 0; $CounterLastDay=$row[' Countertoday ']; $upd _sql= "Update mycounter set recorddate = '$RecordDate', Countertoday = '$CounterToday', Counterlastday = '$CounterLastDay' WHERE id =? ' "; mysql_query($upd _sql); } //get the data again $querysql= "SELECT * from ' mycounter ' WHERE id = '$id‘ "; $queryset=mysql_query($querysql); $Counter=$row[' Counter ']; $CounterToday=$row[' Countertoday ']; $CounterLastDay=$row[' Counterlastday ']; if($row=Mysql_fetch_array($queryset) ) { if($_cookie["User"]! = "Oldguest") {//The same user records only one access record at a time $Counter= ++$row[' Counter ']; $CounterToday= ++$row[' Countertoday ']; $upd _sql= "Update mycounter Set counter = '$Counter', Countertoday = '$CounterToday' WHERE id =$id"; $myquery=mysql_query($upd _sql); } Echo"Total Visits:".$Counter; Echo" "; Echo"Today's traffic:".$CounterToday; Echo" "; Echo"Yesterday's traffic:".$CounterLastDay; } Else{ //If the database is empty, the appropriate action}} showmycounter ();?>
If it is a static page the above method is not possible, but the following is a good example of statistics
Not finished ..... .....
PHP statistics website/html page browse access Count program