Easy-to-use counter (database) _ PHP Tutorial

Source: Internet
Author: User
Easy-to-use counters (databases ). Usage? Include (counter. php); Counter (_ FILE _); add a count for the FILE if ($ PHP_SELFindex.php) {$ countCounter (INDEX_COUNT); add a count for the home page} else {$ count usage
Include ("counter. php ");
Counter (_ FILE _); // add a count for the FILE
If ($ PHP_SELF = "/index. php ")
{
$ Count = Counter ("INDEX_COUNT"); // add a count for the homepage
}
Else
{
$ Count = Counter ("INDEX_COUNT", "", 0); // Get the homepage count
}
Echo "you are the $ count visitor ";
?>
-------- Counter. php -----------
If (! Isset ($ PHP_INCLUDE_COUNTER_PHP ))
{$ PHP_INCLUDE_COUNTER_PHP =__ FILE;

$ Counter_error_state = 0;
$ Counter_error_msg = "";
Function Counter ($ file, $ query = "", $ add = 1)
{
$ Db_name = "database ";
$ Db_user = "username ";
$ Db_pass = "password ";
$ Db_table = "counter ";

If (empty ($ file ))
{
$ Counter_error_state =-100;
$ Counter_error_msg = "The first parameter is missing or the parameter is null ";
Return-100;
}
Global $ PHP_SELF, $ QUERY_STRING, $ counter_error_state, $ counter_error_msg;
If (empty ($ db_user) |! $ Db_user | $ db_user = "") $ res = @ mysql_connect ("localhost ");
Else $ res = @ mysql_connect ("localhost", $ db_user, $ db_pass );
If (! $ Res)
{
$ Counter_error_states =-10;
$ Counter_error_msg = "The database cannot be connected ";
Return-10;
}
If (! @ Mysql_select_db ($ db_name ))
{
$ Counter_error_states =-11;
$ Counter_error_msg = "You cannot select a database ";
Return-11;
}
Else
{
If (! $ Db_res = @ mysql_query ("SELECT * FROM". $ db_table ))
{
If (! $ Db_res = @ mysql_query ("create table ". $ db_table. "(id INTEGER AUTO_INCREMENT, primary key (id), file VARCHAR (255), query VARCHAR (255), time VARCHAR (255), count INT )"))
{
$ Counter_error_states =-20;
$ Counter_error_msg = "a data table cannot be created ";
Return-20;
}
@ Mysql_free_result ($ db_res );
}
$ Str = "SELECT * FROM ". $ db_table. "WHERE file = \"". $ file. "\" AND query = \"". $ query. "\"";

If (! $ Db_res = @ mysql_query ($ str ))
{
$ Counter_error_states =-30;
$ Counter_error_msg = "records cannot be queried ";
Return-30;
}
$ Num = @ mysql_num_rows ($ db_res );
If ($ num> 1)
{
$ Counter_error_states =-40;
$ Counter_error_msg = "unexpected error occurred = data row error ";
Return-40;
}
$ Count = 0;
$ Str = "INSERT ";
$ StrWhere = "";
If ($ num = 1)
{
$ Row = @ mysql_fetch_array ($ db_res );
@ Mysql_free_result ($ db_res );
$ Count = $ row ["count"];
$ Id = $ row ["id"];
$ Str = "UPDATE ";
$ StrWhere = "WHERE id = $ id ";
}
If ($ add <1) return $ count;
$ Count + = $ add;
$ Str. = $ db_table. "SET file = \"". $ file. "\", query = \"". $ query. "\", time = \"". date ("Y; n; d; G; I; s "). "\", count = ". $ count. $ strWhere;
$ Db_res = @ mysql_query ($ str );
If (! $ Db_res)
{
$ Counter_error_states =-50;
$ Counter_error_msg = "records cannot be added or updated ";
Return-50;
}
Return $ count;
}
}

}
?>

Why? Include (counter. php); Counter (_ FILE _); // add a count for the FILE if ($ PHP_SELF =/index. php) {$ count = Counter (INDEX_COUNT); // add a count for the homepage} else {$ count =...

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.