MySQL Basic notes (v)-Practice assignments: Site Statistics exercises

Source: Internet
Author: User

Jobs: Site Statistics

  1, record the user's access information to the file, exclusive line, record the IP address

1<?PHP2 //Site Statistics3     Header(' Content-type:text/html;charset=utf-8 ');4 5 //Get user Information6     $ip=$_server[' REMOTE_ADDR '];7 8 //Write file (append)9     file_put_contents(' Record.text ',$ip." \ r \ n ", file_append);

 2. All information is calculated by the contents of the file, read the entire file, one row for access, and read using the file function.

1 // read data, in units of behavior 2     $info file (' Record.text ');

 3. The total number of accesses is equal to the Total row count, which is the overall number of records of the read array.

1 // Find out the total number of visits to the site 2     $visits Count ($info);

4, find out the current number of accesses, the current user's IP in the number of occurrences of the array

1 //find out the number of times the current user (IP) appears2     $ip _visits= 0;3     foreach($info  as $each _ip)4{//Compare5         if($each _ip==$ip)6             $ip _visits++;7}

 5, the total number of users visited, traverse the current access to all the user information, in addition to use an array to save. If the current IP exists in the array, then do not join, otherwise join. Finally, the new array can be counted.

1 foreach($info  as $each _ip)2     {3         //counts the number of independent IPs owned in the current array4         if(!In_array($each _ip,$unique _ips))5             $unique _ips[] =$each _ip;6 7         //comparison: Read a line from a file, trim off the newline spaces8         if(Trim($each _ip) ==$ip)9             $ip _visits++;Ten  One         //Count the number of elements in the $unique_ips: is the number of independent IP, total users A         $users=Count($unique _ips); -}

6, find out the current user is the number of visitors to visit, as long as the current IP in the independent user array to determine the location of the occurrence.

Here is the code for the entire job:

1<?PHP2 //Site Statistics3     Header(' Content-type:text/html;charset=utf-8 ');4 //Get user Information5     $ip=$_server[' REMOTE_ADDR '];6 //Write file (append)7     file_put_contents(' Record.text ',$ip." \ r \ n ",file_append);8 //read data, in units of behavior9     $info=file(' Record.text ');Ten //Find out the total number of visits to the site One     $visits=Count($info); A //find out the number of times the current user (IP) appears -     $ip _visits= 0; -     $unique _ips=Array(); the     foreach($info  as $each _ip) -     { -         //counts the number of independent IPs owned in the current array -         if(!In_array($each _ip,$unique _ips)) +{//The current new user is added to the standalone IP array -             $unique _ips[] =$each _ip; +             //determines whether the current newly added IP ($each _ip) is the IP of the current user A             if($ip==Trim($each _ip))$user _visit=Count($unique _ips); at  -         } -  -         //comparison: Read a line from a file, trim off the newline spaces -         if(Trim($each _ip) ==$ip) -             $ip _visits++; in  -         //Count the number of elements in the $unique_ips: is the number of independent IP, total users to         $users=Count($unique _ips); +     } - //Demand _ Output the     Echo"Welcome to visit, you are the first {$user _visit} users, the current site has a total of {$users} bit users, *The current web page is visited altogether {$visits} times, you are currently the first {$ip _visits} bit Access "; $?>

Summary of several PHP functions involved in this job:

1,$_server[' remote_addr ']

  $_SERVERis an array of information such as header information (header), path, and script location (scripts locations), and so on. The items in this array are created by the WEB server.

  ' REMOTE_ADDR the IP address of the user who is browsing the current page.

2, file_put_contents - &NBSP; write a string to the file

 1  int file_put_contents  (string   $filename , mixed   $data  [, int  = 0 [, Span style= "color: #0000ff;" >resource   $context ]]) 

and sequentially calls fopen (),fwrite () and fclose () function as well.

Parameters
  filename

The name of the file to be written to.

  data

The data to write. The type can be a string, anarray, or a stream resource (as mentioned above).

If data specified as a stream resource, the cached data stored in stream here is written to the specified file, which is similar to using the stream_copy_to_stream () function.

The argument data can be an array (but not a multidimensional array), which is equivalent to file_put_contents ($filename, Join (", $array)).

  flags

  flagsThe value can be a combination of the following flag using the OR (|) operator.

context

A context resource.

MySQL Basic notes (v)-Practice assignments: Site Statistics exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.