- 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?
<script src="linkcount.js"></script>
<a href="#" onclick="Stat('url or id#')">Test1</a>
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;
}
<?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);
?>
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;