PHP does not need database traffic statistics code

Source: Internet
Author: User
Keywords Web Programming PHP Tutorials
Tags access array based basic close code counter data

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>

Related Article

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.