linux:abettercounter
Differences
This shows you the differences between two versions of the page.
| Previous revision | |||
| — | linux:abettercounter [2022/12/02 21:02] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== 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 " | ||
| + | |||
| + | <code html> | ||
| + | <a href=" | ||
| + | <img src=" | ||
| + | </a> | ||
| + | </ | ||
| + | |||
| + | |||
| + | 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 ' | ||
| + | |||
| + | 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 " | ||
| + | |||
| + | ==== 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: | ||
| + | |||
| + | <code sql> | ||
| + | |||
| + | create database freecounter; | ||
| + | |||
| + | use freecounter; | ||
| + | |||
| + | create table if not exists session | ||
| + | | ||
| + | page tinytext, | ||
| + | sessionid | ||
| + | creation | ||
| + | last datetime, | ||
| + | useragent | ||
| + | ip varchar(50), | ||
| + | pages | ||
| + | |||
| + | create table if not exists counter | ||
| + | (site varchar(250), | ||
| + | | ||
| + | count | ||
| + | | ||
| + | grant select, insert, delete, update | ||
| + | on freecounter.* to freecounter | ||
| + | identified by ' | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | Check that you have done things well: | ||
| + | |||
| + | <code sql> | ||
| + | |||
| + | # 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 " | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | echo( "hello world" ); | ||
| + | |||
| + | ?> | ||
| + | </ | ||
| + | |||
| + | And check that you see it correctly in a web browser. Let's say that your website is " | ||
| + | |||
| + | |||
| + | ==== Check that GD library is installed and working ==== | ||
| + | |||
| + | Edit again the " | ||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | // Create a 100*30 image | ||
| + | $im = imagecreate(100, | ||
| + | |||
| + | // White background and blue text | ||
| + | $bg = imagecolorallocate($im, | ||
| + | $textcolor = imagecolorallocate($im, | ||
| + | |||
| + | // Write the string at the top left | ||
| + | imagestring($im, | ||
| + | |||
| + | // Output the image | ||
| + | header(' | ||
| + | |||
| + | imagepng($im); | ||
| + | imagedestroy($im); | ||
| + | ?> | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | And press the " | ||
| + | |||
| + | ==== And finally the code ==== | ||
| + | |||
| + | |||
| + | And finally, the code. You have to create two pages, '' | ||
| + | |||
| + | === cont2.php === | ||
| + | |||
| + | |||
| + | <code php> | ||
| + | <?php | ||
| + | |||
| + | // create table if not exists session | ||
| + | // (site varchar(250), | ||
| + | // page tinytext, | ||
| + | // sessionid | ||
| + | // creation | ||
| + | // last datetime, | ||
| + | // useragent | ||
| + | // ip varchar(50), | ||
| + | // pages | ||
| + | |||
| + | //create table if not exists counter | ||
| + | // | ||
| + | // page tinytext, | ||
| + | // count | ||
| + | | ||
| + | // grant select, insert, delete, update | ||
| + | // on freecounter.* to freecounter | ||
| + | // identified by ' | ||
| + | |||
| + | // | ||
| + | // pick up request parameters | ||
| + | // | ||
| + | import_request_variables( ' | ||
| + | |||
| + | //$p_site = ' | ||
| + | //$page = ''; | ||
| + | |||
| + | |||
| + | // session time in minutes | ||
| + | $session_time = 30; | ||
| + | |||
| + | // | ||
| + | // session data | ||
| + | // | ||
| + | session_set_cookie_params( $session_time * 60 ); // sessions will last only 30 minutes | ||
| + | |||
| + | session_start(); | ||
| + | |||
| + | $ip = $_SERVER[' | ||
| + | $useragent = $_SERVER[' | ||
| + | |||
| + | $db_host=' | ||
| + | $db_username =' | ||
| + | $db_password ='PUT THE PASSWORD HERE'; | ||
| + | $database =' | ||
| + | |||
| + | $conn = mysql_connect( $db_host, $db_username, | ||
| + | |||
| + | mysql_select_db($database); | ||
| + | |||
| + | |||
| + | set_session_data( $conn, | ||
| + | $p_site, | ||
| + | $p_page, | ||
| + | $ip, | ||
| + | session_id(), | ||
| + | $useragent, | ||
| + | $session_time ); | ||
| + | |||
| + | $counter = get_counter( $conn, $p_site, $p_page ); | ||
| + | |||
| + | output_image( $counter, $ip ); | ||
| + | |||
| + | clear_old_data( $conn ); | ||
| + | |||
| + | mysql_close($conn); | ||
| + | |||
| + | |||
| + | |||
| + | // output a graphic image with the counter value | ||
| + | // and other data | ||
| + | function output_image( $counter, $ip ) | ||
| + | { | ||
| + | |||
| + | $im = imagecreate( 100, 60 ); | ||
| + | | ||
| + | $bg = imagecolorallocatealpha( $im, 0, 0, 0, 127 ); // transparent | ||
| + | $black = imagecolorallocatealpha( $im, 0, 0, 0, 0 ); | ||
| + | | ||
| + | imagestring( $im, 5, 20, 2, $counter, $black ); | ||
| + | | ||
| + | imagestring( $im, 2, 11, 20, 'Your ip is: ', $black ); | ||
| + | | ||
| + | imagestring( $im, 2, 3, 32, $ip, $black ); | ||
| + | | ||
| + | header(' | ||
| + | | ||
| + | imagepng($im); | ||
| + | | ||
| + | imagedestroy( $im ); | ||
| + | | ||
| + | } | ||
| + | |||
| + | // get the counter current value | ||
| + | function get_counter( $conn, $site, $page ) | ||
| + | { | ||
| + | $query = sprintf( " | ||
| + | ." where site = ' | ||
| + | ." and page = ' | ||
| + | mysql_real_escape_string( $site ), | ||
| + | mysql_real_escape_string( $page ) ); | ||
| + | | ||
| + | $result = mysql_query( $query ); | ||
| + | | ||
| + | $row = mysql_fetch_assoc( $result ); | ||
| + | | ||
| + | if( $row ) | ||
| + | return $row[' | ||
| + | else | ||
| + | return 0; | ||
| + | | ||
| + | } // get_counter | ||
| + | |||
| + | // updates the counter data | ||
| + | function set_counter_data( $conn, $site, $page ) | ||
| + | { | ||
| + | $query = sprintf( " | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | $result = mysql_query( $query ); | ||
| + | | ||
| + | $found = false; | ||
| + | $row = mysql_fetch_assoc( $result ); | ||
| + | if( $row ) | ||
| + | { | ||
| + | $found = true; | ||
| + | } | ||
| + | | ||
| + | if( $found ) | ||
| + | { | ||
| + | $update = sprintf( " | ||
| + | ." where site = ' | ||
| + | ." and page = ' | ||
| + | mysql_real_escape_string($site), | ||
| + | mysql_real_escape_string($page) ); | ||
| + | | ||
| + | $result = mysql_query( $update ); | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | $insert = sprintf( " | ||
| + | ." | ||
| + | mysql_real_escape_string($site), | ||
| + | mysql_real_escape_string($page) ); | ||
| + | $result = mysql_query( $insert ); | ||
| + | | ||
| + | } // $found | ||
| + | |||
| + | } // set_counter_data | ||
| + | |||
| + | // all session data older than 1 year is deleted | ||
| + | // from the session table | ||
| + | function clear_old_data( $conn ) | ||
| + | { | ||
| + | $now = strftime( ' | ||
| + | $twoYearAgo = strftime( ' | ||
| + | | ||
| + | $delete = sprintf( " | ||
| + | | ||
| + | | ||
| + | $result = mysql_query( $delete ); | ||
| + | | ||
| + | } // clear old data | ||
| + | |||
| + | // function to update statistical data | ||
| + | // per session: pages visited, user agent | ||
| + | // clicks, time spent | ||
| + | function set_session_data( $conn, $site, $page, $ip, $session, $useragent, $session_time ) | ||
| + | { | ||
| + | |||
| + | $now = strftime( ' | ||
| + | |||
| + | $query = sprintf( " | ||
| + | ." where sessionid = ' | ||
| + | ." and site = ' | ||
| + | ." and page = ' | ||
| + | mysql_real_escape_string($session), | ||
| + | mysql_real_escape_string($site), | ||
| + | mysql_real_escape_string($page) ); | ||
| + | |||
| + | $result = mysql_query( $query ); | ||
| + | |||
| + | | ||
| + | $found = false; | ||
| + | $row = mysql_fetch_assoc( $result ); | ||
| + | if( $row ) | ||
| + | { | ||
| + | $found = true; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | // we will try to find the same ip | ||
| + | // in the last half hour | ||
| + | $a_half_ago = strftime( ' | ||
| + | |||
| + | $query = sprintf( " | ||
| + | ." where ip = ' | ||
| + | ." and creation >= ' | ||
| + | ." and site = ' | ||
| + | ." and page = ' | ||
| + | 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( " | ||
| + | ." useragent = ' | ||
| + | ." ip = ' | ||
| + | ." last = ' | ||
| + | ." | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | $result = mysql_query( $update ); | ||
| + | | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | $insert = sprintf( " | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | $result = mysql_query( $insert ); | ||
| + | |||
| + | set_counter_data( $conn, $site, $page ); | ||
| + | | ||
| + | } | ||
| + | |||
| + | } // set_session_data | ||
| + | |||
| + | |||
| + | |||
| + | ?> | ||
| + | </ | ||
| + | |||
| + | |||
| + | === stats.php === | ||
| + | |||
| + | < | ||
| + | <?php | ||
| + | |||
| + | $month = array( " | ||
| + | " | ||
| + | " | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | ) ); | ||
| + | |||
| + | |||
| + | // | ||
| + | // pick up request parameters | ||
| + | // | ||
| + | import_request_variables( ' | ||
| + | |||
| + | //$p_site = ' | ||
| + | //$p_page = ''; | ||
| + | |||
| + | $db_host=' | ||
| + | $db_username =' | ||
| + | $db_password ='PUT HERE THE PASSWORD'; | ||
| + | $database =' | ||
| + | |||
| + | $conn = mysql_connect( $db_host, $db_username, | ||
| + | |||
| + | mysql_select_db($database); | ||
| + | |||
| + | |||
| + | // INFORMATION TO DISPLAY: | ||
| + | // | ||
| + | // Current value of the counter: XXXXXXX | ||
| + | // Visits | ||
| + | // visited | ||
| + | // Last hour: XXX | ||
| + | // During today: | ||
| + | // Visits during current month: | ||
| + | // Visits during month-1: | ||
| + | // During the year: XXX | ||
| + | // | ||
| + | |||
| + | |||
| + | $now = strftime( ' | ||
| + | $an_hour_ago = strftime( ' | ||
| + | $a_day_ago = strftime( ' | ||
| + | |||
| + | |||
| + | $begin_of_year = strftime( ' | ||
| + | $end_of_year = strftime( ' | ||
| + | |||
| + | $row_year = get_site_stats( $conn, $p_site, $p_page, $begin_of_year, | ||
| + | $row_hour =get_site_stats( $conn, $p_site, $p_page, $an_hour_ago, | ||
| + | $row_day = get_site_stats( $conn, $p_site, $p_page, $a_day_ago, $now ); | ||
| + | |||
| + | |||
| + | for( $i = 0; $i < 12; $i++ ) | ||
| + | { | ||
| + | //echo( "< | ||
| + | |||
| + | $month[" | ||
| + | } // for | ||
| + | |||
| + | |||
| + | |||
| + | ?> | ||
| + | < | ||
| + | |||
| + | < | ||
| + | <meta http-equiv=" | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | |||
| + | <br/> | ||
| + | <br/> | ||
| + | < | ||
| + | <br/> | ||
| + | < | ||
| + | <br/> | ||
| + | <br/> | ||
| + | <br/> | ||
| + | < | ||
| + | <tr> | ||
| + | < | ||
| + | < | ||
| + | <td title=" | ||
| + | <td title=" | ||
| + | </tr> | ||
| + | <?php | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("</ | ||
| + | |||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("</ | ||
| + | |||
| + | for( $i = 0; $i < 12; $i++ ) | ||
| + | { | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("</ | ||
| + | } // for | ||
| + | |||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("< | ||
| + | echo("</ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ?> | ||
| + | </ | ||
| + | <br/> | ||
| + | <br/> | ||
| + | <br/> | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | <?php | ||
| + | |||
| + | mysql_close($conn); | ||
| + | |||
| + | |||
| + | // | ||
| + | // get site statistics in order to display | ||
| + | // in a fancy table | ||
| + | // | ||
| + | function get_site_stats( $conn, $site, $page, $from, $to ) | ||
| + | { | ||
| + | $query = sprintf( " | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | $result = mysql_query( $query ); | ||
| + | | ||
| + | $row = mysql_fetch_assoc( $result ); | ||
| + | | ||
| + | return $row; | ||
| + | |||
| + | } // get_site_stats | ||
| + | |||
| + | // get the counter current value | ||
| + | function get_counter( $conn, $site, $page ) | ||
| + | { | ||
| + | $query = sprintf( " | ||
| + | ." where site = ' | ||
| + | ." and page = ' | ||
| + | | ||
| + | | ||
| + | | ||
| + | $result = mysql_query( $query ); | ||
| + | | ||
| + | $row = mysql_fetch_assoc( $result ); | ||
| + | | ||
| + | if( $row ) | ||
| + | return $row[' | ||
| + | else | ||
| + | | ||
| + | | ||
| + | } // get_counter | ||
| + | | ||
| + | ?> | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Finally, wrap it alltogether ==== | ||
| + | |||
| + | |||
| + | You can make a simple html page to see how it looks like: | ||
| + | |||
| + | <code html> | ||
| + | < | ||
| + | |||
| + | <body bgcolor=" | ||
| + | |||
| + | <a href=" | ||
| + | <img src=" | ||
| + | </a> | ||
| + | |||
| + | </ | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | |||
| + | 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~~ | ||
| + | |||
| + | |||
