links hit counter?

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
I want to count how man times ppl have pressed a link. How can I do that? Are there any ready scripts?
 

kadaver

New Member
Messages
51
Reaction score
0
Points
0
A simple method is using AJAX and php with mysql, atleast this is how I did it, there is probly an even simpler way maby using flat files but mysql is faster.

use AJAX with "onclick" function to record clicks, file with link to be recorded add: "Stat('url or id#')" to "onclick" for any link or hot spot(image/banner)
Example:
HTML:
<script src="linkcount.js"></script>
<a href="#" onclick="Stat('url or id#')">Test1</a>
(edit "src=" to where ever the .js file is and file name of script)
also you only need linkcount.js refrenced once in the page

dont forget the javascript file, source for file "linkcount.js":
(edit "var url=" to where ever the .php file is and file name of script)
Code:
var xmlHttp,statid
function Stat(str)
{ 

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="linkcount.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}function stateChanged() 
{ 
}function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

source for file "linkcount.php":
PHP:
<?php
$url=$_GET["q"];

$Config_host = 'localhost';    //server name (add : and port number for different port)
$Config_user = 'user';         //user name
$Config_password = 'password'; //user password
$Config_db = 'links';      //database to use
$Config_table = 'linkcount';       //table for link data
$Config_where = 'url';         //table column for urls or links

$db = mysql_pconnect($Config_host, $Config_user, $Config_password) or trigger_error(mysql_error(),E_USER_ERROR);

// get server date/time for lastaccess

$t=time();
$ti = date("Y-m-d H:i:s",$t);

// check to see if url is already in database, Part 1

$colname_Recordset1 = "-1";
if (isset($url)) {
  $colname_Recordset1 = (get_magic_quotes_gpc()) ? $url : addslashes($url);
}
mysql_select_db($Config_db, $db);
$query_Recordset1 = sprintf("SELECT * FROM %s WHERE %s = '%s' ", $Config_table, $Config_where, $colname_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $db) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

// Part 2, if no records are found then add new entry

if($totalRows_Recordset1 == "0"){
$insertSQL = sprintf("INSERT INTO %s (id, url, count, lastaccess) VALUES (%s, %s, %s, %s)",
					   $Config_table,
                       GetSQLValueString("", "int"),    //url id #
                       GetSQLValueString($url, "text"), //url
                       GetSQLValueString("1", "int"),   //count
                       GetSQLValueString($ti, "date")); //lastaccess time

  mysql_select_db($Config_db, $db);
  $Result1 = mysql_query($insertSQL, $db) or die(mysql_error());
}else{
// Update count for url
$count = $row_Recordset1['count'] + 1;
$updateSQL = sprintf("UPDATE %s SET count=%s, lastaccess=%s WHERE url=%s",
					   $Config_table,
                       GetSQLValueString($count, "int"),   //count
                       GetSQLValueString($ti, "date"), //lastaccess time
                       GetSQLValueString($url, "text"));

  mysql_select_db($Config_db, $db);
  $Result1 = mysql_query($updateSQL, $db) or die(mysql_error());
}

mysql_free_result($Recordset1);


?>

you can modify the php script according to your uses you can convert the php script to out put flat file too but cant remember how at the moment.

Thought this might help.

----edit forgot to add----
this is the mysql table :
DROP TABLE IF EXISTS `links`.`linkcount`;
CREATE TABLE `links`.`linkcount` (
`id` int(10) unsigned NOT NULL auto_increment,
`url` varchar(255) NOT NULL,
`count` int(10) unsigned NOT NULL,
`lastaccess` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
 
Last edited:

scorch94

Member
Messages
228
Reaction score
0
Points
16
I also made a hit counter once, but it used files instead of MySQL database. It's pretty useless to create one field table just to count hits :)
 
Top