+ Reply to Thread
Results 1 to 4 of 4

Thread: links hit counter?

  1. #1
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    links hit counter?

    I want to count how man times ppl have pressed a link. How can I do that? Are there any ready scripts?

  2. #2
    kadaver's Avatar
    kadaver is offline x10Hosting Member kadaver is an unknown quantity at this point
    Join Date
    Feb 2008
    Location
    US,Indiana
    Posts
    51

    Re: links hit counter?

    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 Code:
    <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 Code:
    <?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 by kadaver; 04-29-2008 at 06:34 PM. Reason: Forgot to add the mysql creat statement

  3. #3
    galaxyAbstractor's Avatar
    galaxyAbstractor is offline Community Advocate galaxyAbstractor is on a distinguished road
    Join Date
    Oct 2007
    Location
    Land of Null and Insanity
    Posts
    5,495

    Re: links hit counter?

    well, sorry. I figured out an nonsql way but thanks anyway

  4. #4
    scorch94 is offline x10 Sophmore scorch94 is an unknown quantity at this point
    Join Date
    Sep 2007
    Posts
    228

    Re: links hit counter?

    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

+ Reply to Thread

Similar Threads

  1. Generating hundreds of links in few days
    By nonte in forum Link Exchange
    Replies: 11
    Last Post: 11-09-2008, 07:55 AM
  2. Ucash.in - Earn Money from links.
    By fabionj in forum Earning Money
    Replies: 1
    Last Post: 03-30-2008, 05:49 PM
  3. Replies: 1
    Last Post: 03-06-2008, 06:15 AM
  4. Replies: 0
    Last Post: 03-05-2008, 08:30 PM
  5. Urgent help please!
    By retro-bliss in forum Free Hosting
    Replies: 2
    Last Post: 10-25-2007, 03:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers