php计数器

发布时间:2021-08-03 22:12:35 阅读:296次

 1.session实现

counter1.php

<body style='font-size:14px;'>
<?php
 session_start();
 $counter=$_SESSION["counter"];
 if($counter==""){
  $counter=1;
  $_SESSION["counter"]=$counter;
 }
 else{
  $counter=$counter+1;
  $_SESSION["counter"]=$counter;
 }
 echo "你是第".$counter."位来访者";
 echo "<input type=button onclick=\"location.href='abandon.php'\" value='重新计数'>";
?>
</body>

abandon.php

<?php
 session_start();
 unset($_SESSION["counter"]);
 session_destroy();
 header("location:counter1.php");
?>

2.cookies实现

counter2.php

<?php
 /* config */
 /*
 
  echo getenv('REMOTE_ADDR')."<br/>";
  echo $_SERVER['PHP_SELF']."<br/>";//取当前文件名
  echo basename($_SERVER['PHP_SELF'])."<br/>";//取当前文件名,区别在于,上一个有"/"
  echo realpath(basename($_SERVER["PHP_SELF"]))."<br/>";//取物理路径;
  print_r($_COOKIE);//输出所有的cookies
 
 */
 /* config */


 $varnum=$_COOKIE["counter"];
 if($varnum==""){
  $varnum=1;
  setcookie("counter", 1, time()+3600);
 }
 else{
  $varnum+=1;
  setcookie("counter", $varnum, time()+3600);
 }
  echo "您是第".$varnum."位造访者";
  echo "<input type=button onclick=\"location.href='clear.php'\" value='重新计数'>";
?>

clear.php

<?php
 setcookie("counter", "timeout", time()-1);
 header("location:counter2.php");
?>

3.防刷新计数器

counter3.php

<?php
 $cn=mysql_connect("localhost","root","123456");
 $db=mysql_select_db("test");
 $sql="update userinfo set age=age+1 where id=1";
 $show="select * from userinfo where id=1";
 $varnum=$_COOKIE["counter"];
 if($varnum!=1)
 {
  $result=mysql_query($sql);
  $result=mysql_query($show);
  $row=mysql_fetch_array($result);
  echo "您是第".$row["age"]."位来访者";
  setcookie("counter",1, time()+10);
 }
 else{
  $result=mysql_query($show);
  $row=mysql_fetch_array($result);
  echo "您是第".$row["age"]."位来访者";
 }
 echo "<input type=button onclick=\"location.href='restart.php'\" value='重新计数'>";
  mysql_free_result($result);
  mysql_close($cn);
?>

restart.php

<?php
 setcookie("counter", "timeout", time()-1);
 header("location:counter3.php");
?>

以下文章可能对你有帮助

若文章对您有帮助,浏览后有收获,为您节省了时间,不妨小额打赏我一下,您的鼓励是维持我不断写博客的最大动力!

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:php计数器 出自老鄢博客 | 欢迎分享