我们在建立网站的时候,有些站长会有在自己首页上添加计数器的需求,我们今天就向大家具体讲讲counter.inc包含以下代码:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> </span></span></li> <li class=""><span>/* </span></li> <li class="alt"><span>|| 一个简单的计数器 </span></li> <li class=""><span>*/ </span></li> <li class="alt"><span>function get_hitcount($counter_file) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span>/* 将计数器归零 </span></li> <li class=""><span>这样如果计数器还未被使用,初始值将是1 </span></li> <li class="alt"><span>你当然也可以把初始值设成20000来骗人咯 </span></li> <li class=""><span>*/ </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">count</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; </span> </li> <li class=""><span>// 如果存放计数器文件已经存在,读取其中的内容 </span></li> <li class="alt"><span>if ( file_exists($counter_file) ) </span></li> <li class=""><span>{ </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">fp</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">fopen</font></span><span>($counter_file,"r"); </span> </li> <li class=""><span>// 我们只取了前20位,希望你的站点不要太受欢迎啊 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">count</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>+fgets($fp,20); </span> </li> <li class=""><span>// 由于函数fgets()返回字符串,我们可以通过加0的方法将其自动转换为整数 </span></li> <li class="alt"><span>fclose($fp); </span></li> <li class=""><span>// 对文件操作完毕 </span></li> <li class="alt"><span>} </span></li> <li class=""><span>// 增加一次计数值 </span></li> <li class="alt"><span>$count++; </span></li> <li class=""><span>// 将新的计数值写入文件 </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">fp</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">fopen</font></span><span>($counter_file,"w"); </span> </li> <li class=""><span>fputs($fp,$count); </span></li> <li class="alt"><span>fclose($fp); </span></li> <li class=""><span># 返回计数值 </span></li> <li class="alt"><span>return ($count); </span></li> <li class=""><span>} </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
PHP添加计数器之后的一步就是我们需要更改front.php3文件以显示这个计数器:
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><strong><font color="#006699"></font></strong></span><span> </span></span></li> <li class=""> <span></span><span class="tag-name"><strong><font color="#006699">include</font></strong></span><span>("include/counter.inc"); </span> </li> <li class="alt"><span>// 我把计数值放在文件counter.txt中,读出并输出 </span></li> <li class=""> <span>printf ("</span><strong><font color="#006699"><span class="tag"><span class="tag-name">CENTER</span><span class="tag">></span><span class="tag"><span class="tag-name">B</span><span class="tag">></span></span></span></font></strong><span>%06d</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">B</span><span class="tag">></span><span class="tag"></span><span class="tag-name">CENTER</span><span class="tag">></span></font></strong><span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">BR</span><span class="tag">></span></span></font></strong><span> n", </span> </li> <li class="alt"><span>get_hitcount("counter.txt")); </span></li> <li class=""><span>include("include/footer.inc"); </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
以上全部代码就是PHP添加计数器的实现方式,希望对有需要的朋友有所帮助。