Home  >  Article  >  Backend Development  >  PHP method to count website visits and output display based on cookies and sessions_php skills

PHP method to count website visits and output display based on cookies and sessions_php skills

WBOY
WBOYOriginal
2016-05-16 20:00:07998browse

The example in this article describes how PHP counts website visits based on cookies and sessions and outputs them for display. Share it with everyone for your reference, the details are as follows:

<&#63;php
$f_open = fopen("count.txt","r+"); //打开指定的文件
$count = fgets($f_open); //读取文件中的数据
if(empty($_COOKIE['cookie_name'])){ //判断COOKIE的是否存在
 setcookie("cookie_name",value,time()+1800); //如果不存在,则创建COOKIE
 $count = $count + 1; //将变量$count的值加1
 rewind($f_open); //打开指定的文件
 fwrite($f_open,$count); //向文件中写入新的数据
 fclose($f_open); //关闭文件
}
&#63;>

<&#63;php session_start(); include("conn/conn.php");
 $data1=date("Y-m-d"); //获取当前访问时间
 $data2=substr(date("Y-m-d"),0,7);
 $ip=getenv('REMOTE_ADDR');
 if($_SESSION[temp]=="" || $_SESSION[temp]==NULL){ //判断$_SESSION[temp]==""的值是否为空,其中的temp为自定义的变量
 //使用数据库存储数据
 $select=mysql_query("select * from tb_count10 where data1='$data1' and ip='$ip'");
 if(mysql_num_rows($select)>0){
  $query1="update tb_count10 set counts=counts+1 where data1='$data1' and ip='$ip'";
  $result1=mysql_query($query1);
 }else{
 $query="insert into tb_count10(counts,data1,data2,ip)values('1','$data1','$data2','$ip')";
    $result=mysql_query($query);
 }
 $_SESSION[temp]=1; //登录以后,$_SESSION[temp]的值不为空,给$_SESSION[temp]赋一个值1
 }
&#63;>

<&#63;php
  //以图形的形式输出数据库中的记录数
  $query="select sum(counts) as counts from tb_count04 ";//查询数据库中总的访问量
  $result=mysql_query($query);
  $visitor=mysql_result($result,0,'counts');
  echo "----------";
  echo "<strong>网站的访问量: </strong>"; //以图形的方式显示访问次数
  //对补位数字0的处理
  $len=strlen($visitor); //获取字符串的长度
  $str=str_repeat("0",6-$len); //获取6-$len个数字0
  for($i=0;$i<strlen($str);$i++){ //获取变量$str的字符串长度
   $result=$str[$i];
   $result='<img src=images/0.gif>';
   echo $result; //循环输出$result的结果
  }
  //对数据库中数据的处理
  for($i=0;$i<strlen($visitor);$i++){ //获取字符串的长度
   $result=$visitor[$i];
   switch($result){
   //如果值为"0",则输出0.gif图片
   case "0"; $ret[$i]="0.gif";break;
   case "1"; $ret[$i]="1.gif";break;
   case "2"; $ret[$i]="2.gif";break;
   case "3"; $ret[$i]="3.gif";break;
   case "4"; $ret[$i]="4.gif";break;
   case "5"; $ret[$i]="5.gif";break;
   case "6"; $ret[$i]="6.gif";break;
   case "7"; $ret[$i]="7.gif";break;
   case "8"; $ret[$i]="8.gif";break;
   case "9"; $ret[$i]="9.gif";break;
   }
   echo "<img src=images/".$ret[$i].".>"; //输出访问次数
}
&#63;>

Readers who are interested in more content related to cookie and session operation in PHP can check out the special topics on this site: "Summary of cookie usage in PHP" and "Summary of session issues in PHP"

I hope this article will be helpful to everyone in PHP programming.

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