建立 count.txt 文本文件和 index.php,准备 1-9 共九个数字的图片,网上可以找到这类图片,分别命名为:1-9
count.txt 文件中,保存访问次数,比如输入100
index.php:
<ol class="dp-c"> <li class="alt"><span><span><?php </span></span></span></li> <li><span> <span class="comment">//选择显示统计数据的颜色</span><span> </span></span></li> <li class="alt"><span> <span class="comment">//$color_name="black_white";</span><span> </span></span></li> <li><span> </span></li> <li class="alt"><span> <span class="vars">$color_name</span><span>=</span><span class="string">"white_black"</span><span>; </span></span></li> <li><span> <span class="comment">//$color_name="black_transparent";</span><span> </span></span></li> <li class="alt"><span> </span></li> <li><span> <span class="comment">// 只读方式打开文件</span><span> </span></span></li> <li class="alt"><span> <span class="vars">$fp</span><span>=</span><span class="func">fopen</span><span>(</span><span class="string">"counter.txt"</span><span>,</span><span class="string">"r"</span><span>); </span></span></li> <li><span> <span class="comment">//读取数据</span><span> </span></span></li> <li class="alt"><span> <span class="vars">$counter</span><span>=</span><span class="func">fgets</span><span>(</span><span class="vars">$fp</span><span>,1024); </span></span></li> <li><span> <span class="comment">//关闭文件</span><span> </span></span></li> <li class="alt"><span> fclose(<span class="vars">$fp</span><span>); </span></span></li> <li><span> <span class="comment">//计数器增加1</span><span> </span></span></li> <li class="alt"><span> <span class="vars">$counter</span><span>++; </span></span></li> <li><span> <span class="comment">//可写方式打开文件</span><span> </span></span></li> <li class="alt"><span> <span class="vars">$fp</span><span>=</span><span class="func">fopen</span><span>(</span><span class="string">"counter.txt"</span><span>,</span><span class="string">"w"</span><span>); </span></span></li> <li><span> <span class="comment">//将新的统计数据写入文本文件</span><span> </span></span></li> <li class="alt"><span> <span class="func">fputs</span><span>(</span><span class="vars">$fp</span><span>,</span><span class="vars">$counter</span><span>); </span></span></li> <li><span> <span class="comment">//关闭文件</span><span> </span></span></li> <li class="alt"><span> fclose(<span class="vars">$fp</span><span>); </span></span></li> <li><span> </span></li> <li class="alt"><span> <span class="comment">//为了防止有其他用户此时也访问了该页面,文件内容有所改变</span><span> </span></span></li> <li><span> <span class="comment">//重新打开文件读取最新统计数据</span><span> </span></span></li> <li class="alt"><span> <span class="vars">$fp</span><span>=</span><span class="func">fopen</span><span>(</span><span class="string">"counter.txt"</span><span>,</span><span class="string">"r"</span><span>); </span></span></li> <li><span> <span class="vars">$counter</span><span>=</span><span class="func">fgets</span><span>(</span><span class="vars">$fp</span><span>,1024); </span></span></li> <li class="alt"><span> fclose(<span class="vars">$fp</span><span>); </span></span></li> <li><span> </span></li> <li class="alt"><span> <span class="comment">//循环将统计数据用图像显示出来</span><span> </span></span></li> <li><span> <span class="comment">//不同的数字针对不同的图像</span><span> </span></span></li> <li class="alt"><span> <span class="comment">//如数字1,则用图像 1.gif来显示</span><span> </span></span></li> <li><span> <span class="keyword">for</span><span> (</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><span class="func">strlen</span><span>(</span><span class="vars">$counter</span><span>);</span><span class="vars">$i</span><span>++) </span></span></span></li> <li class="alt"><span> { </span></li> <li><span> <span class="vars">$result</span><span>=</span><span class="vars">$counter</span><span>[</span><span class="vars">$i</span><span>]; </span></span></li> <li class="alt"><span> </span></li> <li><span> <span class="keyword">switch</span><span>(</span><span class="vars">$result</span><span>) </span></span></li> <li class="alt"><span> { </span></li> <li><span> <span class="keyword">case</span><span> </span><span class="string">"0"</span><span>: </span><span class="vars">$ret</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="string">"0.gif"</span><span>; </span><span class="keyword">break</span><span>; </span><span class="comment">//显示数字图片</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">case</span><span> </span><span class="string">"1"</span><span>: </span><span class="vars">$ret</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="string">"1.gif"</span><span>; </span><span class="keyword">break</span><span>; </span></span></li> <li><span> <span class="keyword">case</span><span> </span><span class="string">"2"</span><span>: </span><span class="vars">$ret</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="string">"2.gif"</span><span>; </span><span class="keyword">break</span><span>; </span></span></li> <li class="alt"><span> <span class="keyword">case</span><span> </span><span class="string">"3"</span><span>: </span><span class="vars">$ret</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="string">"3.gif"</span><span>; </span><span class="keyword">break</span><span>; </span></span></li> <li><span> <span class="keyword">case</span><span> </span><span class="string">"4"</span><span>: </span><span class="vars">$ret</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="string">"4.gif"</span><span>; </span><span class="keyword">break</span><span>; </span></span></li> <li class="alt"><span> <span class="keyword">case</span><span> </span><span class="string">"5"</span><span>: </span><span class="vars">$ret</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="string">"5.gif"</span><span>; </span><span class="keyword">break</span><span>; </span></span></li> <li><span> <span class="keyword">case</span><span> </span><span class="string">"6"</span><span>: </span><span class="vars">$ret</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="string">"6.gif"</span><span>; </span><span class="keyword">break</span><span>; </span></span></li> <li class="alt"><span> <span class="keyword">case</span><span> </span><span class="string">"7"</span><span>: </span><span class="vars">$ret</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="string">"7.gif"</span><span>; </span><span class="keyword">break</span><span>; </span></span></li> <li><span> <span class="keyword">case</span><span> </span><span class="string">"8"</span><span>: </span><span class="vars">$ret</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="string">"8.gif"</span><span>; </span><span class="keyword">break</span><span>; </span></span></li> <li class="alt"><span> <span class="keyword">case</span><span> </span><span class="string">"9"</span><span>: </span><span class="vars">$ret</span><span>[</span><span class="vars">$i</span><span>]=</span><span class="string">"9.gif"</span><span>; </span><span class="keyword">break</span><span>; </span></span></li> <li><span> } </span></li> <li class="alt"><span> } </span></li> <li><span> </span></li> <li class="alt"><span> <span class="func">echo</span><span> </span><span class="string">"该页面的总访问次数为:"</span><span>; </span></span></li> <li><span> <span class="comment">//循环输出图像</span><span> </span></span></li> <li class="alt"><span> <span class="keyword">for</span><span> (</span><span class="vars">$i</span><span>=0;</span><span class="vars">$i</span><span><sizeof><span class="vars">$ret</span><span>);</span><span class="vars">$i</span><span>++) </span></sizeof></span></span></li> <li><span> <span class="func">echo</span><span> </span><span class="string">"<img alt="PHP基于文本的图形显示计数器" >"</span><span>; </span></span></li> <li class="alt"><span>?> </span></li> </ol>