search
Homephp教程php手册php实现计数器方法小结,php计数器小结

php实现计数器方法小结,php计数器小结

本文实例讲述了php实现计数器的方法。分享给大家供大家参考。具体如下:

这里收藏了三款php计数器代码,他们三个都有一个同共点就是全部无需数据库,而是利用了文本文件来实现网页浏览计数.

第一款PHP计数器代码如下:

复制代码 代码如下:

//计数器 
function countx($file="count.dat"){ 
if(file_exists($file)){ 
$fp=fopen($file,"r"); 
$numx=fgets($fp,10); 
fclose($fp); 
$numx++; 
//以上四行代码可以用一条表达式代替:$numx=file_get_contents($file)+1; 

else{ 
$numx=1;} 
file_put_contents($file,$numx);//当文件不存在时,这函数会自动创建文件,而且会自动把参数转成字符串写入。 
echo $numx; 
/*整个函数体可以用两条表达式代替:file_exists($file)?file_put_contents($file,file_get_contents($file)+1):file_put_contents($file,"1");readfile($file); 
*/ 

//函数调用 
countx(); 
?>


第二款PHP计数器,代码如下:

复制代码 代码如下:

  $counterfile = "balong.txt";//存储数值的文件名几路径 
  function displaycounter($counterfile) { 
   $fp = fopen($counterfile,"rw"); 
   $num = fgets($fp,5); 
   $num += 1; 
   print "您是第 "."$num"." 个看巴泷计数器的家伙"; 
   exec( "rm -rf $counterfile"); 
   exec( "echo $num > $counterfile"); 
  } 
   
  if (!file_exists($counterfile)) { 
   exec( "echo 0 > $counterfile"); 
  } 
   
  displaycounter($counterfile); 
?>


第三款PHP计数器代码如下:

复制代码 代码如下:

  $counterfile = "www.jb51.net.txt";//存储数值的文件名几路径 
   
  function displaycounter($counterfile) { 
   $fp = fopen($counterfile,"rw"); 
   $num = fgets($fp,5); 
   $num += 1; 
   print "您是第 "."$num"." 个看巴泷计数器的家伙"; 
   exec( "rm -rf $counterfile"); 
   exec( "echo $num > $counterfile"); 
  }
   
  if (!file_exists($counterfile)) { 
   exec( "echo 0 > $counterfile"); 
  } 
   
  displaycounter($counterfile); 
?>

希望本文所述对大家的php程序设计有所帮助。

Statement
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.