We often see that the Statistical Code of many websites is displayed in the form of js calls. Let's take a look at how the example works.
- <? PHP
- $ Countfile = "num.txt"; // The file written by the counter is count.txt under the current directory. Then, we should test whether the file can be opened.
- If ($ fp = fopen ($ countfile, "r") = false) {// open the file in read/write mode. if the file cannot be opened, exit.
- Printf ("opening file % s failed! ", $ Countfile );
- Exit;
- } Else {
- $ Count = fread ($ fp, 10); // if the file can be opened normally, the data in the file will be read. Assume It is 1
- $ Count = $ count 1; // read 10-Bit Data
- Fclose ($ fp); // close the current file
- $ Fp = fopen ($ countfile, "w"); // open the file in overwrite mode.
- Fwrite ($ fp, $ count); // write new data after 1
- Fclose ($ fp); // close the file
- Echo document. write (". $ count.") n; // output data in javascript format
- }
- ?>
- In the html file, js calls the method.
- <HTML>
- <HEAD>
- <TITLE> example of page separation counter </TITLE>
- </HEAD>
- <BODY>
- Hello, you are the first
- <Script language = "JavaScript" src = "FileCount. php"> // pay attention to the server and directory path of the php counter when referencing. </SCRIPT>
- Visitor
- </BODY>
- </HTML>