Home php教程 php手册 计数器详细设计

计数器详细设计

Jun 21, 2016 am 09:03 AM
count counter nbsp session

概述:
此设计可以在本计数器基础之上设计计数分析程序,可以对页面访问、ip访问次数进行分析,并形成报表。
一、数据库设计
数据库采用mysql
相关文件:    createDatabase.sql        创建数据库
            createTblCounter.sql    创建计数器表


表名:tpCounter(table of pages counter)
字段:
名称    类型    意义
id    Int (10) auto_increment    序列号
pagename    varchar(20)    页面标识,缺省为页面文件名
count    Int(10)    计数值

表名:tiCounter(table of ip counter)
字段:
名称    类型    意义
id    Int(10) auto_increment    序列号
ip    varchar(20)    Ip标识
count    Int(10)    该ip访问次数
date    datetime    最近访问时间
pages    text    曾访问过的页面id,用’|’分隔

二、详细说明:
1、    可以对每个页面进行计数,也可以统计每个ip访问的次数,最近访问时间,以及每次访问的页面,需要两个表;
2、    统计网站访问人次:tpCounter中设置一个站标识[建议用pagename=’0’标志];
3、    每次打开页面时都先检查session,若不存在该用户的session,说明是刚刚开始访问本网站,此时创建一个此用户的session,对网站计数增1,对所访问页面计数增1;[打开或刷新页面时]如果该用户session已存在,网站计数值不增加,但是页面计数值每刷新一次都要增1;
4、    关闭页面时,检查该用户打开本网站页面数是否为0,是则销毁该用户的session,否则不销毁;[此功能不需编写程序,服务器自动执行]
5、    在访问时如果页面在tpCounter中没有标识,自动在表中插入一条记录;
6、    pages是一个文本类型,记录了浏览者访问的时间和访问的页面,其中包含类似这样格式的字符串:
||2001-5-1 16:00:00|1|12|5||2001-8-3 10:12:5|4|9|
表示此ip在2001-5-1 16:00:00访问了1、12、5页面,在2001-8-3  10:12:5访问了5、4、9页面[页面的号从上一个表中获得];
7、    设计计数的文件(.php),每一个页面都包含这个文件,这个文件中包含以下的功能:
        1>session检查,
        2>连接数据库,
        3>计数[参数为 页面名称、ip、当前时间],
        4>读写数据库,
        5>断开与数据库的连接;
8、对所访问的页面的记录采用如下方式:
        用户打开一个新的页面时,如果用户session不存在,写入时间并记录当前页面,若存在,写入当前页面。写入采用附加的方式。
9、    网站计数在此头文件中,对页面计数在所计页面中。
10、每一个页面在包含本文件时,如果要对页面计数,一定要在包含之前使用变量$page_name,并赋值为页面的名称,页面名称不能有重复。

三、接口描述:
相关文件:counter.php

1/Boolean check_session()
功能描述:session检查,原来存在返回true;原来不存在返回false,并创建,并注册布尔型变量existing
入口参数:无
    出口参数:布尔型
2/site_count($content)
    功能描述:网站访问计数
    入口参数:数据库连接
    出口参数:计数值

3/page_count($connect,$page_name,$flag=true)
    功能描述:网页计数,返回页面访问次数,整型,$flag是是否增加计数的标志,缺省true
    入口参数:$connect:数据库连接,$page_name:网页名称
    出口参数:页面访问次数


4/show_site_count(int type)
功能描述:显示计数
入口参数:    type==1采用图形计数
type==2采用文本计数

四、流程
    0/检查进入页面的权限
        由于头文件需要通过引用才可以编译,因此必须检查是通过引用还是直接浏览
1/链接数据库
2/检查session,若不存在,创建session,进行网站计数
3/显示计数
4/进行页面计数
5/断开与数据库的连接[自动实现]

五、使用方法
所有的函数都包含在一个头文件中,在使用时,包含此头文件即可。
六、附源程序
/**    counter.php v1.0
*    by Amio 2001-5-1
*    描述:计数器文件,可以对整个网站计数,
*        可以对所有页面计数,可以对每个ip计数
*/
/**    接口实现功能:
*    1>session 检查
*    2>连接数据库
*    3>计数
*    4>读写数据库
*    5>链接部分的表格输出
*/
/**    使用方法:
*    此文件必须是包含在其他的php文件之中使用,
*    在引用之前需要对$inc变量进行配置
*    e.g.:
*     *    $inc="inc";
*    include("include.php");  
*     
*    ?>
*/
?>
//session检查,返回布尔型
//true--此用户session存在
//false--此用户session不存在
function check_session(){
    $existing=true;
    session_start();
    if (!session_is_registered("existing")){
        session_register("existing");
        return false;     
    } else  return true;     
}


//网页计数,返回页面访问次数,整型
//$flag是是否增加计数的标志,缺省true
function page_count($connect,$page_name,$flag=true){
    $ip = getenv("REMOTE_ADDR");
    $query=@mysql_query("select id,count from tpcounter where pagename='$page_name'",$connect) or die("invalid page query!");
    if (!(mysql_num_rows($query))){
        mysql_query("insert into tpcounter (pagename,count) values('$page_name',1)",$connect)or die("insert page failed");
        $pidquery=@mysql_query("select id from tpcounter where pagename='$page_name'",$connect) or die ("select page id failed");
        $pidarray=mysql_fetch_array($pidquery);
        $pid=$pidarray[id];         
        $return_num=1;
    }else {  
        $array=mysql_fetch_array($query);
        $num=$array[count];         
        $pid=$array[id];         
        if ($flag)
            $num++;
        mysql_query("update tpcounter set count=$num where pagename='$page_name'",$connect)or die("update page failed");
        $return_num=$num;
    }     
    $pquery=@mysql_query("select pages from ticounter where ip='$ip'",$connect) or die ("invalid pages query!");
    if (($flag)&&(mysql_num_rows($pquery))){
        $parray=mysql_fetch_array($pquery);
        $ps="$parray[pages]";
        $pstr="$parray[pages]"."$pid"."|";
        mysql_query("update ticounter set pages='$pstr' where ip='$ip'",$connect)or die ("update ip  failed");
    }  
    return $return_num;  
}

//ip计数,返回ip访问次数,整型
//功能除了计数还有时间更新
//$flag是是否增加计数的标志,缺省true
//注意:ip_count的调用必须在page_count之前!!!
function ip_count($connect){

    $ip = getenv("REMOTE_ADDR");

    $visit_time=date("Y:m:d:H:i");
    $visit_pages="||"."$visit_time"."|";
    $ipquery=@mysql_query("select count,pages from ticounter where ip='$ip'",$connect) or die ("invalid ip query!");
     
    if (!(mysql_num_rows($ipquery))){//新的ip
        $pageStr="|"."$visit_pages";         
        mysql_query("insert into ticounter (ip,count,date,pages) values ('$ip',1,'$visit_time','$pageStr')",$connect)or die("insert ip failed");
        return 1;
    }else{                //旧的ip
        $parray=mysql_fetch_array($ipquery);
        $ipnum=$parray[count];
        $pageStr="$parray[pages]"."$visit_pages";
        $ipnum++;
        mysql_query("update ticounter set count=$ipnum,date='$visit_time',pages='$pageStr' where ip='$ip'",$connect)or die("update ip failed");
        return $ipnum;
    }
         
}

//网站计数,返回整型,网站访问次数
function site_count($connect){
    if (!check_session()){    //session不存在
        $ipnum=ip_count($connect);
        $num=page_count($connect,"website",true);
    }else{            //session存在
        $num=page_count($connect,"website",false);
    }     
    return $num;     
}

function displayCount($num){
    $fileurl="countpng.php?count=".$num;
    return $fileurl;
}

//显示计数值,type为显示类型,length为显示的长度,缺省6
//type=1图形形式
//type=2文本形式(缺省)
function show_site_count($num,$length=6,$type=2){

    $outStr=strval($num);
    for ($i=strlen($outStr)+1;$i        $outStr="0"."$outStr";
    }
    switch ($type){
        case 1:
            echo " echo displayCount($outStr);
            echo "\">";
            break;
        case 2:
        default:     
            echo "$outStr";     
    }     
}
?>

if (!isset($inc))exit;
$connect=mysql_connect('localhost','root','');//connect to server
mysql_select_db("damio",$connect); //select database ,database name is damio

$sitecount=site_count($connect);
if (isset($page_name))
    page_count($connect,$page_name);
?>



Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to Fix Activation Error Code 0xc004f069 in Windows Server How to Fix Activation Error Code 0xc004f069 in Windows Server Jul 22, 2023 am 09:49 AM

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using

See all articles