Detailed counter design _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Detailed counter design. Overview: This design allows you to design a count analysis program based on this counter. you can analyze the page access and ip address access times and generate reports. I. Database Design Overview of database collection:
This design allows you to design a count analysis program based on this counter to analyze page access and ip address access times and form a report.
I. database design
The database uses mysql
Related Files: createDatabase. SQL create a database
CreateTblCounter. SQL create counter table


Table name: tpCounter (table of pages counter)
Field:
Name Type meaning
Id Int (10) auto_increment serial number
Pagename varchar (20) page id. the default value is the page file name.
Count Int (10) count value

Table name: tiCounter (table of ip counter)
Field:
Name Type meaning
Id Int (10) auto_increment serial number
Ip varchar (20) Ip ID
Count Int (10) number of visits from this ip address
Date datetime recent access time
Pages text previously accessed page IDs, separated by '|'

II. Detailed description:
1. you can count each page, or count the number of visits to each ip address, the last visit time, and the two tables required for each visit;
2. Count website visits: Set a website id in tpCounter [pagename = '0' flag is recommended];
3. check the session when you open the page. if the user's session does not exist, it indicates that the user has just started accessing the website. at this time, create a session for the user and increase the website count by 1, increase the number of Accessed pages by 1. [when you open or refresh the page] if the user's session already exists, the website count value does not increase, but the page count value increases by 1 for each refresh;
4. when the page is closed, check whether the number of pages opened by the user is 0. If yes, the user's session is destroyed; otherwise, the user's session is not destroyed. [no program is required for this function, automatic server execution]
5. if the page is not identified in tpCounter during access, a record is automatically inserted into the table;
6. pages is a text type that records the access time and Page accessed by the browser, including strings in a format similar to this:
| 16:00:00 | 1 | 12 | 5 | 4 | 9 |
It indicates that this ip address has accessed pages 1, 12, and 5 at 16:00:00, and accessed pages 5, 4, and 9 at [the page number is obtained from the previous table];
7. design the counting file (. php). every page contains this file, which includes the following functions:
1> session check,
2> connect to the database,
3> count [the parameter is the page name, ip address, and current time],
4> read and write databases,
5> disconnect from the database;
8. use the following method to record the Accessed page:
When a user opens a new page, if the user session does not exist, write the time and record the current page. If yes, write the current page. Write is appended.
9. The website count is in this header file, and the page count is in the counted page.
10. when a page contains this file, if you want to count the page, you must use the variable $ page_name before it is included, and assign a value to the page name. The page name cannot be repeated.

III. Interface description:
Related Files: counter. php

1/Boolean check_session ()
Function description: session check. true is returned for existence; false is returned for absence, and a Boolean variable existing is created and registered.
Entry parameter: None
Exit parameter: Boolean
2/site_count ($ content)
Function description: website access count
Entry parameter: database connection
Exit parameter: count value

3/page_count ($ connect, $ page_name, $ flag = true)
Function description: page count, number of page visits returned, integer type, $ flag indicates whether to increase the count. the default value is true.
Entry parameter: $ connect: database connection, $ page_name: webpage name
Exit parameter: Page access count


4/show_site_count (int type)
Function description: Display count
Entry parameter: type = 1
Type = 2 use text count

IV. process
0/check the permissions on the page
Because the header file can be compiled only by reference, you must check whether the file is referenced or directly browsed.
1. connect to the database
2. check the session. if the session does not exist, create the session and count the website.
3/display count
4/count pages
5. disconnect from the database [automatically implemented]

5. usage
All functions are included in a header file. you can include this header file when using this function.
VI. source program attachment
/** Counter. php v1.0
* By Amio 2001-5-1
* Description: counter file, which can be used to count the entire website,
* You can count all pages and each ip address.
*/
/** Interface implementation functions:
* 1> session check
* 2> connect to the database
* 3> count
* 4> read/write databases
* 5> link table output
*/
/** Usage:
* This file must be included in other PHP files,
* You need to configure the $ inc variable before referencing it.
* E.g .:
* * $ Inc = "inc ";
* Include ("include. php ");
*
*?>
*/
?>
// Session check, returns a boolean value
// 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;
}


// Webpage count, return page access times, integer
// $ Flag indicates whether to increase the count. the default value 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 count, integer
// In addition to counting, the function also has time to update.
// $ Flag indicates whether to increase the count. the default value is true.
// Note: ip_count must be called before 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 address
$ 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 address
$ 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;
}

}

// Website count, return integer type, number of website visits
Function site_count ($ connect ){
If (! Check_session () {// The 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 the count value. type indicates the display type and length indicates the display length. the default value is 6.
// Type = 1 graphical form
// Type = 2 text format (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 );
?>

Based on this counter, the counter analysis program can be designed to analyze page access and ip address access times and generate reports. I. database design database collection...

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.