Home arrow MySQL arrow Page 3 - Logging with PHP

Logging with PHP

In this article, Tim shows us how to log hits on all of your web sites. They can even be on separate servers around the world.

Author Info:
By: Tim Perdue
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
February 06, 2003
  1. · Logging with PHP
  2. · The Design
  3. · Source Code
  4. · Conclusion

print this article

Logging with PHP - Source Code
(Page 3 of 4 )


  //gif11.php accepts these parameters:
$c; //correlates to fld_affil_num in the database -
   //unique for each site
$s; //correlates to fld_special in the database
$b; //random number - forces the gif to load, even if cached
   //generated by PHP and discarded


//don't have any extra spaces outside the <? ?>
//  or you could have a broken gif come back

Header( "Content-type: image/gif");
passthru( "cat clear.gif" ); //send a 1x1 gif

$logger_action=5;  //impression
$logger_special=$s; //move the $s param for the logger
$fld_affil_num=$c; //ditto for the $c param

include('connect.inc'); //connect to the DB SERVER
include('logger.inc'); //Exanded below


PHP Code to generate the GIF URL:


<IMG SRC="http://www.yourserver.com/util/gif11.php?c=4&s=phpbuildercom&b=<?
echo $random_num; ?>" height=1 width=1>



//Connect to the PostgreSQL database

$conn = pg_pconnect("user=myname dbname=my_db");

//I use a persistent connection instead of opening
//and closing the connection tens of thousands
//of times/day

if (!$conn) {
  echo "An error occured.\n"; //amateur error detection




//ACTIONS - 1=get 2=click-thru 3=clickin 4=jump2zip 5=impression

if (!$logger_action) {echo "error in logger"; exit;}
if (!$logger_special) {echo "error in logger"; exit;}

//The following should be on one long line...

$logger_sql="insert into tbl_activity_log values (".date("Ymd", mktime()).", ". //PHP method to build date
  "'".date("H", mktime())."', ". //PHP method to get hour
  "'$REMOTE_ADDR', ".     //Get the IP address of client
  "$logger_action, ' ".    //action as set up in gif11.php
  "$logger_special', ".    //ditto
  "$fld_affil_num);";     //which web site

$res_logger = pg_Exec ($conn, $logger_sql);

if (!$res_logger) {
  echo "An error occured in the logger.\n";
  echo pg_ErrorMessage();




$goto_location="Location: ".$goto."&b=".$b;


  $logger_action=2; //adclick





//This is just a tiny sample of the reporting you can do

Function ShowResults($result) {

//I apologize to the author of this code -
//I believe I found it in the code exchange, but I
//can't find it now! --Tim

  if ($result) {
    $rows = pg_NumRows($result);
    $cols = pg_NumFields($result);

    echo( "<table border=1>\n");
    /* Create the headers */
    echo( "<tr>\n");
    for($i = 0; $i < $cols; $i++) {
      printf( "<th>%s</th>\n", pg_FieldName($result, $i));
    echo( "</tr>");
    for($j = 0; $j < $rows; $j++) {
      echo( "<tr>\n");
      for($i = 0; $i < $cols; $i++) {
        printf( "<td>%s</td>\n", pg_result($result, $j, $i));
      echo( "</tr>");
    echo( "</table>");
  } else {

//The following should be one long line

$report_sql="SELECT fld_date, fld_special, count(*) AS IMPRESSIONS ".
  "FROM tbl_activity_log ".
  "WHERE fld_action=5 AND fld_affil_num=$affil ".
  "GROUP BY fld_date, fld_special;";

$res_click_report = pg_Exec ($conn, $report_sql);

if ((!$res_click_report) || (pg_NumRows($res_click_report) < 1)) {
  echo "An error occured.\n";




blog comments powered by Disqus

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials