The problem is that after I implement statistical counting myself, I want to use its output in my page that does not support PHP functions. So how should I do it? The main solution is to output a javascript script. This is very useful, such as implementing dynamic news and advertising polling. Of course, here is how to output the download count of a certain program to a non-PHP page.
Let’s imagine this: I already have a program that downloads a count file on my website, such as ../count/download.db.
The file format is:
Index|File name hint|Link|Count
Note that they are separated by "|" lines here. I use text files for processing. There may be a line of data in it:
file1|test file 1|../download/file1.zip|10
As you can see, the number of downloads may have been 10 times. Now I want to output this 10 times to other pages.
Step 1: Write Javascript
Very simple:
Isn’t it! What follows src refers to the output script program, and what follows "?" refers to the parameters passed into the script. So what data should output.php
output before it can be executed? JavaScript statements should be output, such as document.write() and the like. In this way, the browser will treat the output of output.php
as a JavaScript program and process it, just like a directly written script, except that this script is obtained from other places
.
OK, now that you know what content should be output, you can write a php program.
Step 2: Output results
$fp=fopen("../count/download.db", "r");
$flag =FALSE;
while(!feof($fp))
{
$line=fgets($fp, 256);
list($index, $title, $url, $count) =split("|", $line);
if (strtolower($index)==strtolower($id))
{
$flag=TRUE;
break;
}
}
fclose($fp);
if ($flag)
{
echo "document.write($count);";
}
else
echo "document.write("not found");";
?>
This code is also very simple, but there are a few points to explain. First open a file. $flag indicates whether the file record of the specified index is found.
is first set to FALSE. Another loop, the condition is that the file has not ended.
Then comes the loop body: take out a line of text, preferably longer. To split fields, use "|" as the delimiter. Note that the split function used
is a regular expression, and "|" is a special symbol used with the "" sign. Then put them into the corresponding variables respectively. What we really care about here is
$index and $count. Compare whether the input parameter $id and the retrieved index ($index) are equal. If they are equal, set the found flag to TRUE and
exit the loop. Otherwise, find the next row of data.
At the end, close the file and output the corresponding javascript script based on whether the mark is found.
For the use of $id, PHP automatically processes the called URL?id=xxx and can be used directly. You can also use
$HTTP_GET_VARS[id].