Here is a question for all you PHP wizards.
I have a referral script I'm using. It lists the last 100 referrals to my web page. I would like to include a timestamp next to each link, so people can see what time they came through.
If anyone can help, I would appreciate it greatly.
Right now, the output would be as follows, with each link linked to that url.
http://search.yahoo.com/bin/search?p=lunch+box
I want something like this:
http://search.yahoo.com/bin/search?p=lunch+box Oct. 10, 2001 - 12:45:05
I would prefer that the time not be linked to the referral URL, but if it had to, that's ok.
Here is the link to download the complete script, if you want to play around with it.
http://littlegreenfootballs.com/weblog/scripts/download.php?x=lgf-referrers.zip
I have customized this script so that after 85 characters, it appends a "...". This was to stop very long URL from stretching the screen forever.
Here is the PHP file that controls the output:
<?php
$reflog = "reflog.txt";
?>
<?php
$rfile = file($reflog);
foreach ($rfile as $r) {
$r = chop($r);
if(strlen($r) > 85) {
$ar = substr($r, 0, 85) . "...";
}
else {
$ar = $r;
}
if ($r <> "Direct request") echo "
<table width=\"100%\"cellspacing=0 cellpadding=0 border=0>
<tr>
<td width=14><img width=14 height=16 src=/home/images/bullet_box.gif alt=\"\"></td>
<td width=5 > </td>
<td align=left class=standardtext><a href=\"$r\" onMouseOver=\"window.status='View Site'; return true\" onMouseOut=\"window.status=' '\">$ar</a></td>
</tr>
</table>
<img src=/home/images/spacer.gif width=1 height=4 alt=\"\" border=0><br>
\n";
}
?>
Here is the backbone:
<?php
/*
LGF Referrer Log
By Charles F. Johnson
Copyright 2001 LGF Web Design
All Rights Reserved.
http://littlegreenfootballs.com
This file may be freely distributed
as long as the credits and copyright
message above remain intact.
--------------------------------------------
*/
// Name of referrer log file
$reflog = 'reflog.txt';
// Name of semaphore file
$semaphore = 'semaphore.ref';
// Maximum number of referrers to log
$maxref = 100;
// Domain name of this site (minus "http://www.")
$mydomain = 'lunchboxpad.com';
// From whence did Bunky come?
$ref = getenv("HTTP_REFERER");
// Cover me. I'm going in.
if (($ref) and (!strstr($ref, $mydomain))) { // if there's a referrer, and it's not someone bouncing around this site
$ref .= "\n"; // append a line feed
$sp = fopen($semaphore, "w"); // open the semaphore file
if (flock($sp, 2)) { // lock the semaphore; other processes will stop and wait here
$rfile = file($reflog); // read the referrer log into an array
if ($ref <> $rfile[0]) { // if this referrer is different from the last one
if (count($rfile) == $maxref) // if the file is full
array_pop($rfile); // pop the last element
array_unshift($rfile, $ref); // push the new referrer onto the front
$r = join("", $rfile); // make the array into a string
$rp = fopen($reflog, "w"); // open the referrer log in write mode
$status = fwrite($rp, $r); // write out the referrer URLs
$status = fclose($rp); // close the log
}
}
$status = fclose($sp); // close the semaphore (and release the lock)
}
?>
Bryan