Ekzemplo In the preceding example, because the Ekzemplo link has a *ping* attribute, a browser that supports click pings will request this ping link after loading the linked page: http://cadenhead.org/pingtrack.php?url=http://ekzemplo.com Click tracking is common on web sites, implemented through browser redirects that interpose a script between the original page and the linked page. The click ping adds support for this process to HTML. Here's a short example of how it could be used in a PHP script: // load this file require('poplink.php'); // create a Poplink object $pop = new Poplink(); // get an array containing data for the 100 most clicked links $popular = $pop->get_popular_links(100); Version: 0.1 Web: http://workbench.cadenhead.org/poplink Copyright (C) 2006 Rogers Cadenhead This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ class Poplink_Core { /* Sample poplink.php settings: // the machine hosting the MySQL server var $database_server = "localhost"; // the MySQL database var $database_name = "poplink"; // the MySQL user with access to the database var $database_user = "zeppo"; // the MySQL user password var $database_password = "swordfish"; */ // EDIT VARIABLES BELOW THIS LINE AT YOUR OWN RISK var $software_version = "0.1"; var $last_error = ""; var $debug = false; var $DEBUG_ONLY = true; // set up a Poplink object function Poplink() { // no constructor setup necessary } // report errors function report_error($message, $debug_only = false) { if (($debug_only) & (!$this->debug)) return; error_log("Poplink: " . $message); } // retrieve last error message function get_last_error() { return $this->last_error; } // connect to the MySQL database function connect_to_database() { $db = mysql_connect($this->database_server, $this->database_user, $this->database_password); if (!$db) { $this->report_error("Could not connect to database."); return false; } else { mysql_select_db($this->database_name); return true; } } // lock database function lock_table($read_only = false) { $query = "LOCK TABLES $this->database_name WRITE"; if ($read_only) { $query = "LOCK TABLES $this->database_name READ"; } $result = mysql_query($query); } // unlock database function unlock_table() { $query = "UNLOCK TABLES"; $result = mysql_query($query); } // create tables function create_database_tables() { $output = ""; if (!$this->connect_to_database()) return; $poplink_query = "CREATE TABLE poplink(" . "dex MEDIUMINT UNSIGNED AUTO_INCREMENT PRIMARY KEY," . "link TINYTEXT," . "visits MEDIUMINT UNSIGNED DEFAULT 0," . "status SET('allowed', 'banned') DEFAULT 'allowed'" . ")"; $output .= "

Attempting to create poplink database ... "; $result = mysql_query($poplink_query); if ($result == false) { $output .= "failed"; } else { $output .= "done"; } return $output; } // retrieve data for a pinged link, creating a new record if necessary function get_link($url) { if ($url == "") return false; $url = $this->get_query_safe_text($url); if (!$this->connect_to_database()) return false; $query = "SELECT * FROM poplink WHERE link = '$url'"; $result = mysql_query($query); if ($result == false) { $this->report_error(mysql_error() . ": $query"); return false; } if (mysql_num_rows($result) < 1) { // add a new link if ($this->add_link($url) === true) { $this->get_link($url); } } else { // retrieve the link $url_data = mysql_fetch_array($result); $url_data['visits'] = $url_data['visits'] + 1; $url_data['link'] = $this->get_slash_decoded_text($url_data['link']); $query = "UPDATE poplink SET visits = {$url_data['visits']} WHERE link = '$url'"; $result = mysql_query($query); if ($result == false) { $this->report_error(mysql_error() . ": $query"); return false; } return $url_data; } } // add a new link to the database function add_link($url) { if ($url == "") return false; $url = $this->get_query_safe_text($url); if (!$this->connect_to_database()) return false; $link = $url; $visits = 0; $status = 'allowed'; $query = "INSERT INTO poplink VALUES(0, '$link', '$visits', '$status')"; $result = mysql_query($query); if ($result == false) { $this->report_error(mysql_error() . ": $query"); return false; } return true; } // get an array containing the most clicked links function get_popular_links($limit = 100) { $query = "SELECT * FROM poplink ORDER BY visits DESC LIMIT $limit"; if (!$this->connect_to_database()) return; $result = mysql_query($query); if ($result == false) { $this->report_error(mysql_error() . ": $query"); // create database if it doesn't exist yet $this->create_database_tables(); return false; } $links = array(); $count = 0; while ($row = mysql_fetch_array($result)) { $links[$count] = $row; $count++; } return $links; } // the functions after this point are used within the class and should not be called externally // make text safe for MySQL database query function get_query_safe_text($text) { $text = $this->get_slash_decoded_text($text); $text = mysql_real_escape_string($text); return $text; } // strip slashes from database text if necessary function get_slash_decoded_text($text) { if (get_magic_quotes_gpc()) { $text = stripslashes($text); } return $text; } // add slashes to database text if necessary function get_slash_encoded_text($text) { if (!get_magic_quotes_gpc()) { return addslashes($text); } else { return $text; } } }