PHP实现通过文本文件统计页面访问量功能示例

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP实现通过文本文件统计页面访问量功能。分享给大家供大家参考,具体如下:

一 代码

index.php

<?php session_start();
if($_SESSION[temp]==""){ //判断$_SESSION[temp]==""的值是否为空,其中的temp为自定义的变量
    if(($fp=fopen("counter.txt","r"))==false){
        echo "打开文件失败!";
    }else{
        $counter=fgets($fp,1024);    //读取文件中数据
        fclose($fp);          //关闭文本文件
        $counter++;           //计数器增加1
        $fp=fopen("counter.txt","w");  //以写的方式打开文本文件<!---->
        fputs($fp,$counter);      //将新的统计数据增加1
        fclose($fp);
    } //关闭文
    $_SESSION[temp]=1; //登录以后,$_SESSION[temp]的值不为空,给$_SESSION[temp]赋一个值1
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>通过文本文件统计页面访问量</title>
<style type="text/css">
<!--
.STYLE1 {
    font-size: 12px;
    font-weight: bold;
}
body {
    margin-left: 0px;
    margin-top: 0px;
    margin-right: 0px;
    margin-bottom: 0px;
}
.STYLE2 {
    color: #FF0000;
    font-weight: bold;
}
-->
</style>
</head>
<body>
<table width="995" height="809" border="0" align="center" cellpadding="0" cellspacing="0" background="images/bg.jpg">
 <tr>
  <td width="131" height="215"> </td>
  <td width="714"> </td>
  <td width="128"> </td>
 </tr>
 <tr>
  <td height="323"> </td>
  <td align="center" valign="top"><table width="660" height="323" border="0" cellpadding="0" cellspacing="0" background="images/bg3.jpg">
   <tr>
    <td width="671" height="420"><p>  <span class="STYLE1">
     <p class="STYLE1"><strong>企业精神</strong>:博学、创新、求实、笃行</p>
     <p class="STYLE1"><strong>公司理念</strong>:以高新技术为依托,战略性地开发具有巨大市场潜力的高价值的产品。</p>
     <p class="STYLE1"><strong>公司远景</strong>:成为拥有核心技术和核心产品的高科技公司,在某些领域具有领先的市场地位。</p>
     <p class="STYLE1"><strong>核心价值观</strong>:永葆创业激情、每一天都在进步、容忍失败,鼓励创新、充分信任、平等交流。</p></td>
   </tr>
   <tr>
    <td height="40" align="center"><img src="gd1.php" /></td>
   </tr>
  </table></td>
  <td> </td>
 </tr>
 <tr>
  <td> </td>
  <td> </td>
  <td> </td>
 </tr>
</table>
<p> </p>
</BODY>
</HTML>

gd1.php

<?php
//以图形的形式输出数据库中的记录数
if(($fp=fopen("counter.txt","r"))==false){
    echo "打开文件失败!";
}else{
    $counter=fgets($fp,1024);
    fclose($fp);
    //通过GD2函数创建画布
    $im=imagecreate(240,24);
    $gray=imagecolorallocate($im,255,255,255);
    $color =imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)); //定义字体颜色
    //输出中文字符
    $text=iconv("gb2312","utf-8","网站的访问量:"); //对指定的中文字符串进行转换
    $font = "Fonts/FZHCJW.TTF";
    imagettftext($im,14,0,20,18,$color,$font,$text); //输出中文
    //输出网站的访问次数
    imagestring($im,5,160,5,$counter,$color);
    imagepng($im);
    imagedestroy($im);
}
?>

二 运行结果

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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

相关文章

使用ETags减少Web应用带宽和负载第1/2页

介绍 最近,大众对于REST风格应用架构表现出强烈兴趣,这表明Web的优雅设计开始受到人们的注意。现在,我们逐渐理解了“3W架构(Architecture of the World W...

php实现的SSO单点登录系统接入功能示例分析

本文实例讲述了php实现的SSO单点登录系统接入功能。分享给大家供大家参考,具体如下: SSO英文全称Single Sign On,单点登录。SSO是在多个应用系统中,用户只需要登录一次...

PHP5.6.8连接SQL Server 2008 R2数据库常用技巧分析总结

PHP5.6.8连接SQL Server 2008 R2数据库常用技巧分析总结

本文实例讲述了PHP5.6.8连接SQL Server 2008 R2数据库常用技巧。分享给大家供大家参考,具体如下: 用到的工具: 1.JetBrains PhpStorm (编写p...

php utf-8转unicode的函数第1/2页

UTF编码 UTF-8就是以8位为单元对UCS进行编码。从UCS-2到UTF-8的编码方式如下: UCS-2编码(16进制) UTF-8 字节流(二进制) 0000 - 007F 0xx...

PHP实现根据时间戳获取周几的方法

本文实例讲述了PHP实现根据时间戳获取周几的方法。分享给大家供大家参考,具体如下: 获取某个时间戳的周几,以及未来几天以后的周几  其中: $time 代表时间  $...