FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Follow the user





garf
click.php
Code:
<?php
// Click counter
$day = date("Y-m-d"); // format the date like it is written in the database
if ($QUERY_STRING) {
   include("db.php"); // connect to the database
   $url = urldecode($QUERY_STRING);
   $result = mysql_query("SELECT * FROM clickout WHERE url='$url' AND day='$day'",$db);
   if(mysql_num_rows($result) > 0) {
      mysql_free_result($result);
      $result = mysql_query("SELECT UNIX_TIMESTAMP(time) FROM ip WHERE (url='$url' AND ipnum='$REMOTE_ADDR' AND day='$day')",$db);
      if($row = mysql_fetch_row($result)) {
            if(($row[0]+3600) < time()) {
               mysql_query("UPDATE clickout SET raw=raw+1,uni=uni+1 WHERE url='$url' AND day='$day'",$db);
               mysql_query("UPDATE ip SET time=NOW() WHERE url='$url' AND ipnum='$REMOTE_ADDR' AND day='$day'",$db);
            } else { // the click is raw
               mysql_query("UPDATE clickout SET raw=raw+1 WHERE url='$url' AND day='$day'",$db);
            }
      mysql_free_result($result);
      } else { // it's a new ip
         mysql_query("UPDATE clickout SET raw=raw+1,uni=uni+1 WHERE url='$url' AND day='$day'",$db);
         mysql_query("INSERT INTO ip (url,ipnum,day) VALUES ('$url','$REMOTE_ADDR','$day')",$db);
      }
   } else { // it's a new url or day
      mysql_query("INSERT INTO ip (url,ipnum,day) VALUES ('$url','$REMOTE_ADDR','$day')",$db);
      mysql_query("INSERT INTO clickout (url,day,raw,uni) VALUES ('$url','$day',1,1)",$db);
   }

   $url="Location: ".$url;
   header($url);
   exit;
}
?>

db.php
Code:

<?php
$dbhost      =   "localhost";
$dbuser      =   "user";
$dbpasswd   =   "password";
$db=mysql_connect("$dbhost","$dbuser","$dbpasswd");
mysql_select_db("count",$db);
?>

index.html
Code:
<html>
<body bgcolor="#FFFFFF">
<a href="click.php?http://slashdot.org/">A test link...</a><br>
<a href="click.php?http://www.slashdot.org/">Another test link...</a><br>
<a href="click.php?http://slashdot.com/">Jet another one...</a><br>
<a href="click.php?http://www.slashdot.com/">You won't believe, it's a link...</a><br>
<p>
<a href="click.php?stats.php">Click Statistics</a>
</p>
</body>
</html>

stats.php
Code:

<html>
<head>
<title>Click Statistics</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1250">
<style type="text/css">
<!--
td {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt; text-decoration: none}
p {  font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt; text-decoration: none}
a {  font-family: Verdana, Arial, Helvetica, sans-serif; font-weight: bold; text-decoration: none}
-->
</style></head>

<body bgcolor="#999999" text="#333333" link="#9999FF" vlink="#3399FF" alink="#0099FF">
<table width="90%" border="0" cellspacing="2" cellpadding="2" align="center" bgcolor="#000000">
  <tr bgcolor="#FFFFCC">
    <td>Click statistics...</td>
  </tr>
  <tr bgcolor="#FFFFCC">
    <td>
    <p>This are the statistics for all your pages grouped by days...</p>
         <?php
         include("db.php"); // connect to the base


         $lastday = " ";
   $result = mysql_query("SELECT * FROM clickout ORDER BY day DESC",$db);
   while($row = mysql_fetch_array($result)) {
      if(!strstr($lastday,$row['day'])) {

         if($lastday != " ") { print("</table><br>\n"); } // if it is empty don't terminate the table because it does not exist
         // table formatting
         print("<table width=\"80%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\" align=\"center\" bgcolor=\"#009999\">\n");
         printf("<tr><td bgcolor=\"#009999\" colspawn=\"3\" align=\"center\"><b><font color=\"#FFFFCC\">%s</font></b></td></tr>",$row['day']);
         print("<tr><td><b><font color=\"#FFFFCC\">URL</font></b></td><td><b><font color=\"#FFFFCC\">raw</font></b></td><td><b><font color=\"#FFFFCC\">unique</font></b></td></tr>");

      }
      printf("<tr><td bgcolor=\"#FFFFCC\">%s</td><td bgcolor=\"#FFFFCC\">%s</td><td bgcolor=\"#FFFFCC\">%s</td></tr>",$row['url'],$row['raw'],$row['uni']);
      $lastday = $row['day'];
   }
   ?>
       </table>
    </td>
  </tr>
</table>
</body>
</html>

dump.sql
Code:
# phpMyAdmin MySQL-Dump
# http://phpwizard.net/phpMyAdmin/
#
# Host: localhost Database : count
# --------------------------------------------------------
#
# Table structure for table 'clickout'
#
CREATE TABLE clickout (
   day date DEFAULT '0000-00-00' NOT NULL,
   url varchar(120) NOT NULL,
   raw int(11) NOT NULL,
   uni int(11) NOT NULL
);


# --------------------------------------------------------
#
# Table structure for table 'ip'
#
CREATE TABLE ipclick (
   day date DEFAULT '0000-00-00' NOT NULL,
   url varchar(120) NOT NULL,
   time timestamp(14),
   ipnum varchar(15) NOT NULL
);

ml
wohooooohooo

another 3 pages of code.
it would be nice if you told us the next time, what this code does Confused
DX-Blog
It keeps track of links being clicked, both unique and raw hits. Unique is concidered once a day.
shabda
But then wont it be so much better if the poster gave a detailed description of what and how it works.
James007
A lot of code... okay... Why? You can post a whole php library if you want but that is called spamming and isn't allowed... Rolling Eyes
DX-Blog
shabda wrote:
But then wont it be so much better if the poster gave a detailed description of what and how it works.

Indeed, seeing as to how the people which can read the code anyways will understand it and dun need to rip codes like this off the internet, creating them yourself allows you to modify it much better into your content.

For the people which dunno how to code it themselves this just comes falling out of the air basically.
Related topics
ftp wont work
Windows Tips&tricks!
Google Ads help fund Spyware ?
Spam warnings...
2 good Customer Support jokes!!!
Point to the user online
How To : Improve Your PHP Programming
have problem on my sql
Mail with PHP (Simple Tutorial)
350 x 20 User Bar + Tutorial
Dynamic User Customizable Sites. CSS + PHP = Awesome!
Private folders in XP
email notification or follow option for particular user
How do I count the number of downloads?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.