Counter detailed design

Source: Internet
Author: User
Tags exit contains header include insert connect table name variable
Overview:
This design can be designed on the basis of the counter analysis program, the page access, the number of IP visits can be analyzed, and form a report.
First, the database design
Database with MySQL
Related files: Createdatabase.sql CREATE Database
Createtblcounter.sql Create counter Table


Table name: Tpcounter (Table of pages counter)
Field:
Name Type meaning
ID Int (TEN) auto_increment serial number
PAGENAME varchar (20) page identification, default to page filename
Count Int (10) Counting value

Table name: Ticounter (Table of IP counter)
Field:
Name Type meaning
ID Int (TEN) auto_increment serial number
IP varchar IP ID
Count Int (10) The number of IP accesses
Date DateTime Recent Access time
Page ID, which has been visited by pages text, with ' | ' Separated

Second, detailed description:
1, can be counted on each page, you can also count the number of each IP access, the most recent access time, as well as each visit to the page, need two tables;
2, Statistics website visitors: Tpcounter set a station logo [recommended pagename= ' 0 ' logo];
3, every time the page is opened to check the session, if there is no session of the user, the description is just beginning to visit this site, at this time to create a session of this user, the site count increased by 1, to the number of pages accessed 1;[open or Refresh page. If the user session already exists, the site count value does not increase, but the page count value is increased by 1 per refresh;
4, close the page, check whether the user opened this Site page number is 0, is to destroy the user's session, otherwise not destroyed; [This feature does not need to write programs, the server automatically executes]
5, in the visit if the page is not identified in the Tpcounter, automatically insert a record in the table;
6. Pages is a text type that records the time that the viewer accesses and the pages it accesses, which contain strings similar to those in this format:
|| 2001-5-1 16:00:00|1|12|5| | 2001-8-3 10:12:5|4|9|
Indicates that this IP visited 1, 12, 5 pages in 2001-5-1 16:00:00, and in 2001-8-3 10:12:5 visited 5, 4, 9 pages [the number of pages obtained from the previous table];
7, design count of the file (. php), each page contains this file, this file contains the following features:
1>session Check,
2> Connection Database,
3> count [parameter is page name, IP, current time],
4> Read and write databases,
5> disconnect from the database;
8, the access to the pages of the record by the following way:
When a user opens a new page, if the user session does not exist, write time and record the current page, if present, write to the current page. Write in an additional way.
9, the site count in this header file, the page count on the page counted.
10, every page in the inclusion of this file, if you want to count the page, be sure to use the variable $page_name before the inclusion, and assign the name of the page, the page name can not be duplicated.

Third, the interface description:
Related documents: counter.php

1/boolean check_session ()
Function Description: Session check, the original existence returns true; returns False, does not exist, and creates and registers a Boolean variable existing
Entry parameters: None
Export parameter: Boolean type
2/site_count ($content)
Function Description: Web site access Count
Entry Parameters: Database connection
Exit Parameters: Count value

3/page_count ($connect, $page _name, $flag =true)
Function Description: Page count, return page access times, Integer, $flag is whether to increase the count of flags, the default True
Entry parameters: $connect: Database connection, $page _name: web name
Export parameters: page access times


4/show_site_count (int type)
Feature Description: Display count
Entry parameters: Type==1 with graph count
type==2 with text count

Four, process
0/Check Access to page
Because the header file needs to be compiled by reference, you must check to see whether it is by reference or by direct browsing
1/LINK Database
2/Check session, if not present, create session, make site count
3/Display Count
4/Page Count
5/disconnect from the database [auto Implementation]

V. Methods of Use
All functions are included in a header file, which is included in the header file when used.
Vi. Source-attached procedure
-->/** counter.php v1.0
* by Amio 2001-5-1
* Description: Counter file, can be counted on the entire site,
* Can count on all pages, can count for each IP
*/
/** Interface Implementation function:
* 1>session Inspection
* 2> Connection Database
* 3> Count
* 4> Read and write database
* 5> the link part of the table output
*/
/** Use Method:
* This file must be included in other PHP files for use,
* The $INC variable needs to be configured before referencing
* e.g.:
* -->* $inc = "Inc";
* Include ("include.php");
*
*?>
*/
?>
-->Session Check, return Boolean
true--This user session exists
false--This user session does not exist
function Check_session () {
$existing =true;
Session_Start ();
if (!session_is_registered ("existing")) {
Session_register ("existing");
return false;
else return true;
}


Page count, return page access times, integral type
$flag is the flag to increase the count, the default is 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 count, return IP access times, integral type
Feature has time to update except counting
$flag is the flag to increase the count, the default is True
Note: Ip_count calls must precede 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))) {//New 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{//Old 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;
}

}

Site count, return integral type, site access times
function Site_count ($connect) {
if (!check_session ()) {//session does not exist
$ipnum =ip_count ($connect);
$num =page_count ($connect, "website", true);
}else{//session exists
$num =page_count ($connect, "website", false);
}
return $num;
}

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

Display count value, type to display, length to display, default 6
Type=1 graphic Form
type=2 text form (default)
function Show_site_count ($num, $length =6, $type =2) {

$outStr =strval ($num);
For ($i =strlen ($outStr) +1; $i <= $length; $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);
?>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.