====== Making a professional counter in PHP ====== **How to make your your own counter, and free!!!** This page explains how to create a professional php counter in your page, or how to use mine to put a counter in your page. Mine is a real free counter that I will keep hosted in this pages as far as I maintain them. ===== How to put the counter in your page ===== If your website is called "www.example.com", you will have to cut and paste the following code snippet in your page: Suppose you want to have many counters in your page; let's say that one is for a public site, other is for the mailing lists, other is for whatever: you can use the 'page' parameter to personalize the counters you want to have. If you are only interested in the use of the counter, you are done. If you want to make your own counter, please keep reading. ===== Things you must have ===== In order that the following code works properly, you have the following in your website: * a running mysql database (I won't consume much resources of it, but I need a space to keep track of the sessions and the counter values) * permission to run php in your web site (later we will go over this) * the "gd" graphics libary installed in your system ==== Setting up the database ==== It is desirable to have a mysql database with a user with select, update, delete and insert permission over two tables. Here is the code you have to execute as root in order to create things properly: create database freecounter; // or the name you want to give to this use freecounter; create table if not exists session (site varchar(250), page tinytext, sessionid varchar(50), creation datetime, last datetime, useragent tinytext, ip varchar(50), pages int(16)); create table if not exists counter (site varchar(250), page tinytext, count int(16)); grant select, insert, delete, update on freecounter.* to freecounter identified by 'CHOOSE A PASSWORD HERE'; Check that you have done things well: # mysql -u freecounter -p password: CHOOSE A PASSWORD HERE mysql> use freecounter; mysql> select * from session; mysql> select * from counter; ==== Test that php is running in your site ==== Create a file "hello.php" with the following: And check that you see it correctly in a web browser. Let's say that your website is "www.example.com". So, to see this page you should type in a web browser the following: ''http://www.example.com/hello.php''. And you should see the message "hello world". If not, check things. ==== Check that GD library is installed and working ==== Edit again the "hello.php" and change by the following: And press the "refresh" button of your browser or open a new one. You should see again the message "Hello world", but this time is a picture showing this message instead of a message (if you don't understand what I'm saying, probably you shouldn't have to continue reading some points ago). ==== And finally the code ==== And finally, the code. You have to create two pages, ''cont2.php'' and ''stats.php''. Yes, I could have an include page to put the functions, but I do not want to have all the things properly one: an opportunity to you to enhace things a bit. However, there are the source code: === cont2.php === = '%s' " ." and site = '%s' " ." and page = '%s'", mysql_real_escape_string($ip), mysql_real_escape_string($a_half_ago), mysql_real_escape_string($site), mysql_real_escape_string($page) ); $result = mysql_query( $query ); $row = mysql_fetch_assoc( $result ); if( $row ) { $found = true; } // $row } // $row if( $found ) { $update = sprintf( "update session set pages = pages + 1, " ." useragent = '%s', " ." ip = '%s', " ." last = '%s' " ."where site = '%s' and page = '%s' and sessionid = '%s'" , mysql_real_escape_string( $useragent ), mysql_real_escape_string( $ip ), mysql_real_escape_string( $now ), //last mysql_real_escape_string( $site ), // site mysql_real_escape_string( $page ), mysql_real_escape_string( $session ) ); // $result = mysql_query( $update ); } else { $insert = sprintf( "insert into session (site, page, sessionid, creation, last, useragent, ip, pages) " ." values( '%s', '%s', '%s', '%s', '%s', '%s', '%s', %s )", mysql_real_escape_string( $site ), // site mysql_real_escape_string( $page ), mysql_real_escape_string( $session ), // sessionid mysql_real_escape_string( $now ), // creation mysql_real_escape_string( $now ), // last mysql_real_escape_string( $useragent ), // useragent mysql_real_escape_string( $ip ), // ip mysql_real_escape_string( 1 ) ); $result = mysql_query( $insert ); set_counter_data( $conn, $site, $page ); } } // set_session_data ?> === stats.php === array( 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre', 'End of Year' ), "value" => array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ), "date" => array( strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 1, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 2, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 3, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 4, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 5, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 6, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 7, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 8, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 9, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 10, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 11, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 12, 1, date("Y",mktime())+1) ), strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 12, 31, date("Y",mktime())+1) ) ) ); // // pick up request parameters // import_request_variables( 'G', 'p_' ); //$p_site = 'www.example.com'; //$p_page = ''; $db_host='localhost'; $db_username ='freecounter'; $db_password ='PUT HERE THE PASSWORD'; $database ='freecounter'; $conn = mysql_connect( $db_host, $db_username, $db_password ); mysql_select_db($database); // INFORMATION TO DISPLAY: // // Current value of the counter: XXXXXXX // Visits Average pages Average time // visited spent // Last hour: XXX YYY ZZZ // During today: XXX YYY ZZZ // Visits during current month: XXX YYY ZZZ // Visits during month-1: XXX YYY ZZZ // During the year: XXX YYY ZZZ // $now = strftime( '%G-%m-%d %H:%M:%S %z', mktime() ); $an_hour_ago = strftime( '%G-%m-%d %H:%M:%S %z', mktime() - (60*60) ); $a_day_ago = strftime( '%G-%m-%d %H:%M:%S %z', mktime() - (24*60*60) ); $begin_of_year = strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 1, 1, date("Y",mktime())+1) ); $end_of_year = strftime( '%G-%m-%d %H:%M:%S %z', mktime( 0, 0, 0, 12, 31, date("Y",mktime())+1) ); $row_year = get_site_stats( $conn, $p_site, $p_page, $begin_of_year, $now ); $row_hour =get_site_stats( $conn, $p_site, $p_page, $an_hour_ago, $now ); $row_day = get_site_stats( $conn, $p_site, $p_page, $a_day_ago, $now ); for( $i = 0; $i < 12; $i++ ) { //echo( "

".$month["name"][$i]." ".$month["value"][$i].$month["date"][$i]."

" ); $month["stats"][$i] = get_site_stats( $conn, $p_site, $p_page, $month["date"][$i], $month["date"][$i+1] ); } // for ?>


Valor actual del contador:


\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); for( $i = 0; $i < 12; $i++ ) { echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); } // for echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); echo("\n\r"); ?>
Visitas Paginas visitadas Tiempo por visita
La última hora".$row_hour['visits']."".round($row_hour['avg_pages'],1)."".round($row_hour['avg_time_spent']/60,1)."
La últimas 24 horas".$row_day['visits']."".round($row_day['avg_pages'],1)."".round($row_day['avg_time_spent']/60,1)."
".$month['name'][$i]."".$month['stats'][$i]['visits']."".round($month['stats'][$i]['avg_pages'],1)."".round($month['stats'][$i]['avg_time_spent']/60,1)."
En todo el año".$row_year['visits']."".round($row_year['avg_pages'],1)."".round($row_year['avg_time_spent']/60,1)."



Contador proporcionado por www.supermanhamuerto.com
==== Finally, wrap it alltogether ==== You can make a simple html page to see how it looks like: And that's all. Enjoy, and if you think that this is useful, please comment out here or recommend to your friends and colleagues. ~~DISCUSSION~~