php计数器

 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");
?>

    A+
发布日期:2021年08月03日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: