We want to provide the following PHP tutorial flow Statistics Code is the basic text file without a database tutorial, below we start from simple to complex page access count counter code instance.
<HTML>
<HEAD>
<TITLE> File System based page Counter example </TITLE>
</HEAD>
<BODY>
? PHP
$countfile = "Num.txt";
Defines that the file written by the counter is count.txt in the current directory, and then we should test whether the file can be opened
if ($fp = fopen ($countfile, "intermolecular")) = = False) {//Open file in read-write mode, exit if not open
printf ("Open file%s failed!", $countfile);
Exit;
}
Else
{
If the file can be opened normally, read the data in the file, assuming it is 1
$count = Fread ($fp, 10);
Read 10-bit data
$count = $count + 1;
Fclose ($FP);
Close the current file
$fp = fopen ($countfile, "w+");
Open files in overwrite mode
Fwrite ($fp, $count);
Write new data plus 1
Fclose ($FP);
and close the file
Echo ' Hello, you are the first '. $count. ' Visitors ';
}
?>
</BODY>
</HTML>
Counter two, this base database
<HTML>
<HEAD>
<TITLE> Database-based page Counter example </TITLE>
</HEAD>
<BODY>
? PHP
$db =mysql tutorial _connect ("localhost", "root", "qwaszx");
$query = "SELECT * from num";
$result =mysql_db_query ("test", $query);
if ($result) {
$r =mysql_fetch_array ($result);
$counter = $r ["NumValue"];
$counter = $counter +1;
$query = ' Update num set numvalue= '. $counter. ' Where id=1 ';
$result =mysql_db_query ("test", $query);
Mysql_close ();
Echo ' Hello, you are the first '. $counter. ' Visitors ';
}
?>
</BODY>
</HTML>
Iii. Basic Text File
? PHP
$countfile = "Num.txt";
Defines that the file written by the counter is count.txt in the current directory, and then we should test whether the file can be opened
if ($fp = fopen ($countfile, "intermolecular")) = = False) {//Open file in read-write mode, exit if not open
printf ("Open file%s failed!", $countfile);
Exit;
}
Else
{
If the file can be opened normally, read the data in the file, assuming it is 1
$count = Fread ($fp, 10);
Read 10-bit data
$count = $count + 1;
Fclose ($FP);
Close the current file
$fp = fopen ($countfile, "w+");
Open files in overwrite mode
Fwrite ($fp, $count);
Write new data plus 1
Fclose ($FP);
and close the file
echo document.write ("". $count. "") n ";
Output data in JavaScript tutorial
}
?>
Call method
<HTML>
<HEAD>
<TITLE> Page Separation Counter Example </TITLE>
</HEAD>
<BODY>
Hello, you are the first
<script language= "JavaScript" src= "filecount.php" >
Note the server and directory path where the PHP counter resides.
</SCRIPT>
Guest
</BODY>
</HTML>
Method four uses the custom picture
<HTML>
<HEAD>
<TITLE> Graphics Page Counter Example </TITLE>
</HEAD>
<BODY>
? PHP
$countfile = "Num.txt";
Defines that the file written by the counter is count.txt in the current directory, and then we should test whether the file can be opened
if ($fp = fopen ($countfile, "intermolecular")) = = False) {//Open file in read-write mode, exit if not open
printf ("Open file%s failed!", $countfile);
Exit;
}
Else
{
If the file can be opened normally, read the data in the file, assuming it is 1
$count = Fread ($fp, 10);
Read 10-bit data
$count = $count + 1;
Fclose ($FP);
Close the current file
$fp = fopen ($countfile, "w+");
Open files in overwrite mode
Fwrite ($fp, $count);
Write new data plus 1
Fclose ($FP);
and close the file
}
Code $count converted to picture mode
$fp = fopen ($countfile, "R"); Open a file in read-only mode
$array _count = 1; Defines a variable that represents the position of an array element, with the following
while (! feof ($fp)) {
$current _number = fgetc ($FP);
$counter _array[$array _count] = $current _number;
$array _elements = count ($counter _array);
$array _count = $array _count + 1;
}
Display code
Echo ("<table border=" 0 "height=" 5 "align=" left "><tr><td align=" center "> Welcome you, section");
for ($array _id = 1; $array _id < $array _elements + + $array _id) {
Echo ("<img src="./img/$counter _array[$array _id].gif "align=absmiddle>");
}
Echo ("Guest </td></tr></table>");
?>
</BODY>
</HTML>