search
HomeBackend DevelopmentPHP Tutorial计数器详细设计_PHP

计数器详细设计_PHP

Jun 01, 2016 pm 12:27 PM
connectipcountcounterdesignaccessdetailedpage

计数器

概述:
此设计可以在本计数器基础之上设计计数分析程序,可以对页面访问、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
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
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool