Home Backend Development PHP Tutorial How to use PHP to create a dynamic counter_PHP tutorial

How to use PHP to create a dynamic counter_PHP tutorial

Jul 15, 2016 pm 01:31 PM
php Instructions specific make dynamic exist us Add to of Web page counter

我们在创建网站的时候,通常都会有在网页中添加计数器的功能,今天在这篇文章中我们将为大家具体介绍PHP制作动态计数器的相关功能。

的功能非常强大,你可以利用它做几乎任何事。不需要复杂的变量和代码,你就可以非常迅速地做出漂亮的计数器来。下面,就让我们一步一步的来做。

其实,PHP制作动态计数器的原理大家都知道,首先,确定一个记录文件,例如counter.txt或counter.log,每一次访问这个页面,就对这个文件加1,然后把结果显示出来。因此,我们的PHP代码开始应该是这样的:

<ol class="dp-xml">
<li class="alt"><span><span class="tag">< ?</span><span class="tag-name">PHP</span><span> </span></span></li><li><span>$</span><span class="attribute">countfile</span><span> = </span><span class="attribute-value">"js/counter.inf"</span><span>;  </span></li><li class="alt"><span>//定义计数器写入的文件是当前目录下JS目录中<br />//counter.inf,然后我们应当测试该文件能否打开  </span></li><li><span>if (($</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fopen</span><span>($countfile, "r+")) == false) <br />{ //用读写模式打开文件,若不能打开就退出  </span></li><li class="alt"><span>printf ("Open file %s failed!",$countfile);  </span></li><li><span>exit;  </span></li><li class="alt"><span>}  </span></li><li><span>else  </span></li><li class="alt"><span>{  </span></li><li><span>//如果文件能够正常打开,就读入文件中的数据,假设是1  </span></li><li class="alt"><span>$</span><span class="attribute">count</span><span> = </span><span class="attribute-value">fread</span><span> ($fp,10);  </span></li><li><span>//读取10位数据  </span></li><li class="alt"><span>$</span><span class="attribute">count</span><span> = $count + 1;  </span></li><li><span>//count ++  </span></li><li class="alt"><span>fclose ($fp);  </span></li><li><span>//关闭当前文件  </span></li><li class="alt"><span>$</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fopen</span><span>($countfile, "w+")  </span></li><li><span>//以覆盖模式打开文件  </span></li><li class="alt"><span>fwrite ($fp,$count);  </span></li><li><span>//写入加1后的新数据  </span></li><li class="alt"><span>fclose ($fp);  </span></li><li><span>//并关闭文件  </span></li><li class="alt"><span>} </span></li></ol>
Copy after login

这时,整个PHP制作动态计数器的工作就完成了,如果只是简单的文字计数的话,在这里就可以输出变量$count的值。下面是将$count转换为图片模式的代码:

<ol class="dp-xml"><li class="alt"><span><span>$</span><span class="attribute">fp</span><span> = </span><span class="attribute-value">fopen</span><span> ($countfile, "r"); <br />//以只读模式打开文件  </span></span></li><li><span>$</span><span class="attribute">array_count</span><span> = </span><span class="attribute-value">1</span><span>; //定义一个表<br />示数组元素位置的变量,下面要用  </span></li><li class="alt"><span>while (! feof($fp)) {  </span></li><li><span>$</span><span class="attribute">current_number</span><span> = </span><span class="attribute-value">fgetc</span><span>($fp);  </span></li><li class="alt"><span>$counter_array[$array_count] = $current_number;  </span></li><li><span>$</span><span class="attribute">array_elements</span><span> = </span><span class="attribute-value">count</span><span> ($counter_array);  </span></li><li class="alt"><span>$</span><span class="attribute">array_count</span><span> = $array_count + 1;  </span></li><li><span>}  </span></li></ol>
Copy after login

上面这个while循环的作用是把每一位数分离出来。它从counter.inf中由左到右逐位读取数值,并依次写入一个叫counter_array的数组中,这个数组的索引是从1开始的($array_count)。如果现在counter.inf中的数字是158,那么数组$counter_array[]就像这样:$counter_array[1] = 1、$counter_array[2] = 5、$counter_array[3] = 8。有了上面这些工作,我们就可以方便地显示不同的数字图片了,PHP制作动态计数器显示代码如下:

<ol class="dp-xml"><li class="alt"><span><span>echo ("</span><span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">border</span><span>=($%$43%^<br />#ASD#2@$#f$%^)0($%$43%^#ASD#2@$#f$%^)<br /> </span><span class="attribute">height</span><span>=($%$43%^#ASD#2@$#f$%^)5($%$43<br />%^#ASD#2@$#f$%^) </span><span class="attribute">align</span><span>=($%$43%^#ASD#2@$<br />#f$%^)center($%$43%^#ASD#2@$#f$%^)</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span></span></li>
<li class="alt"><span><span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">bgcolor</span><span>=($%$43%^#ASD#2@$#f$%^)#<br />bab389($%$43%^#ASD#2@$#f$%^) </span></span></li><li class="alt"><span><span class="attribute">align</span><span>=($%$43%^#ASD#2@$#f$%^)center($%$43%^#ASD#2@$#f$%^)</span></span></li><li class="alt"><span><span class="tag">></span><span>欢迎您,第");  </span></span></li>
<li>
<span>for ($</span><span class="attribute">array_id</span><span> = </span><span class="attribute-value">1</span><span>;$array_id </span><span class="tag"><</span><span> $array_elements; ++ $array_id) {  </span></li><li class="alt"><span>echo ("</span><span class="tag"><</span><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">http</span><span>://www.asp315.com<br />/news/2/($%$43%^#ASD#2@$#f$%^)images<br />//counter//$counter_array[$array_id].gif($%$43%^#ASD#2@$#f$%^)<br /> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">absmiddle</span><span class="tag">></span><span>");  </span>
</li>
<li><span>}  </span></li>
<li class="alt">
<span>echo ("位客人</span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span>");  </span>
</li>
<li><span>}  </span></li>
</ol>
Copy after login

上面的PHP制作动态计数器代码很简单,就是画一个表格,并依次在表格中显示所需的图片。在imagescounter目录下有0.gif至9.gif十张图片,for循环遍历数组,从高位到低位给出每一位数相应的图片,直到数组的尾部。这样,一个完整的计数器就完成了。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446242.htmlTechArticle我们在创建网站的时候,通常都会有在网页中添加计数器的功能,今天在这篇文章中我们将为大家具体介绍PHP制作动态计数器的相关功能。...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles