Log in

View Full Version : PHP Hit/Visitor Log



bjf98
03-08-2009, 01:22 PM
I'm trying to create a PHP hit log. My object is to track my entire site, and sort the results into six different tables: Home page, Images page, Downloads page, About page, Other pages (I run a blog, so other pages would be the posts) and Admin visits. The hits are stored in a mysql database. Here is my current code:

Report Page


<HTML><HEAD></HEAD><BODY>
<script type="text/javascript">

//JK Popup Window Script (version 3.0)- By JavaScript Kit (http://www.javascriptkit.com)
//Visit JavaScriptKit.com for free JavaScripts
//This notice must stay intact for legal use

function openpopup(popurl){
var winpops=window.open(popurl,"","width=1000,height=800,location,status,scrollbars")
}
</script>
<CENTER><H1>REPORT OF HITS</H1></CENTER>
<?PHP


function ConnectDB(){ // connects to database
$conn = mysql_connect('*********', '********, '********') or die('Could not connect: ' . mysql_error());
mysql_select_db('********') or die('Could not select database');
return $conn;
}

function ShowBrief(){

echo"<br>";
echo"<h3>Home</h3>";

echo "<TABLE border=1><THEAD><TR><TH>ID</TH><TH>DATE</TH><TH>TIME</TH><TH>IP</THEAD><TBODY>";
$results=mysql_query("SELECT * FROM log WHERE l_page='fogarty4.co.cc' AND l_remoteIP!='************'");
while ($row = mysql_fetch_array($results)) {
echo "<TR><TD>$row[0]</TD><TD>$row[1]</TD><TD>$row[2]</TD><TD><a href='javascript:openpopup('http://cqcounter.com/whois/?query=$row[3]&map=1')'>$row[3]</a></TD></TR>";
}
echo "</TBODY>";

echo"<br>";
echo"<h3>Images</h3>";

echo "<TABLE border=1><THEAD><TR><TH>ID</TH><TH>DATE</TH><TH>TIME</TH><TH>IP</THEAD><TBODY>";
$results2=mysql_query("SELECT * FROM log WHERE l_page='fogarty4.co.cc/images' AND l_remoteIP!='************'");
while ($row2 = mysql_fetch_array($results2)) {
echo "<TR><TD>$row2[0]</TD><TD>$row2[1]</TD><TD>$row2[2]</TD><TD><a href='javascript:openpopup('http://cqcounter.com/whois/?query=$row[3]&map=1')'>$row2[3]</a></TD></TR>";
}
echo "</TBODY>";

echo"<br>";
echo"<h3>Downloads</h3>";

echo "<TABLE border=1><THEAD><TR><TH>ID</TH><TH>DATE</TH><TH>TIME</TH><TH>IP</THEAD><TBODY>";
$results3=mysql_query("SELECT * FROM log WHERE l_page='fogarty4.co.cc/downloads' AND l_remoteIP!='************'");
while ($row3 = mysql_fetch_array($results3)) {
echo "<TR><TD>$row3[0]</TD><TD>$row3[1]</TD><TD>$row3[2]</TD><TD><a href='javascript:openpopup('http://cqcounter.com/whois/?query=$row[3]&map=1')'>$row3[3]</a></TD></TR>";
}
echo "</TBODY>";

echo"<br>";
echo"<h3>About</h3>";

echo "<TABLE border=1><THEAD><TR><TH>ID</TH><TH>DATE</TH><TH>TIME</TH><TH>IP</THEAD><TBODY>";
$results4=mysql_query("SELECT * FROM log WHERE l_page='fogarty4.co.cc/about' AND l_remoteIP!='************'");
while ($row4 = mysql_fetch_array($results4)) {
echo "<TR><TD>$row4[0]</TD><TD>$row4[1]</TD><TD>$row4[2]</TD><TD><a href='javascript:openpopup('http://cqcounter.com/whois/?query=$row[3]&map=1')'>$row4[3]</a></TD></TR>";
}
echo "</TBODY>";

echo"<br>";
echo"<h3>Other</h3>";

echo "<TABLE border=1><THEAD><TR><TH>ID</TH><TH>DATE</TH><TH>TIME</TH><TH>IP</THEAD><TBODY>";
$results4=mysql_query("SELECT * FROM log WHERE l_page!='fogarty4.co.cc' OR l_page!='fogarty4.co.cc/images' OR l_page!='fogarty4.co.cc/downloads' OR l_page!='fogarty4.co.cc/about' AND l_remoteIP!='************'");
while ($row4 = mysql_fetch_array($results4)) {
echo "<TR><TD>$row4[0]</TD><TD>$row4[1]</TD><TD>$row4[2]</TD><TD><a href='javascript:openpopup('http://cqcounter.com/whois/?query=$row[3]&map=1')'>$row4[3]</a></TD></TR>";
}
echo "</TBODY>";

echo"<br>";
echo"<h3>Admin</h3>";

echo "<TABLE border=1><THEAD><TR><TH>ID</TH><TH>DATE</TH><TH>TIME</TH><TH>IP</THEAD><TBODY>";
$results6=mysql_query("SELECT * FROM log WHERE l_remoteIP = '************'");
while ($row6 = mysql_fetch_array($results6)) {
echo "<TR><TD>$row6[0]</TD><TD>$row6[1]</TD><TD>$row6[2]</TD><TD><a href='javascript:openpopup('http://cqcounter.com/whois/?query=$row[3]&map=1')'>$row6[3]</a></TD></TR>";
}
echo "</TBODY>";
}

$conn=ConnectDB();
ShowBrief();
$conn->close;
?>
</BODY></HTML>

Database Creation Page


<?PHP

function ConnectDB(){ // connects to database

$conn = mysql_connect('*********', '*********', '*********') or die('Could not connect: ' . mysql_error());

mysql_select_db('*********') or die('Could not select database');

return $conn;

}

function MakeDB(){

$conn=ConnectDB();

$query="drop table log;"; $results=mysql_query($query);

$query="create table log (l_index int auto_increment primary key,l_date date, l_time char(8), l_remoteIP varchar(15), l_page longtext);";

$results=mysql_query($query);

$conn->close;

echo "Complete.";

}

$conn=ConnectDB();

MakeDB();

$conn->close;

?>


Tracking Code


<?PHP
function ConnectDB(){ // connects to database
$conn = mysql_connect('*********', '*********', '*********') or die('Could not connect: ' . mysql_error());
mysql_select_db('*********') or die('Could not select database');
return $conn;
}
function WriteLog(){
$remoteIP=$_ENV["REMOTE_ADDR"];
$current = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$query="INSERT INTO log SET l_date=CURDATE(), l_time=CURTIME(), l_remoteIP=\"$remoteIP\", l_page=\"$current\"";
$results=mysql_query($query);
}
$conn=ConnectDB();
WriteLog();
$conn->close;
?>


I starred out my mysql connection and my IP

If anyone could help me that would be great.
Thanks in advance.

JasonDFR
03-08-2009, 02:25 PM
Is something not working? Are you having trouble figuring out how do to something? What help do you need? What questions do you have?

bjf98
03-10-2009, 10:30 PM
Thanks for your reply.
The results are put into the database correctly, but I cannot seem to get the results page to display them. So I am guessing that the only code malfunctioning is report.php. If anyone could help me build a page that outputs the results as described below, I would be very grateful.
First, the database layout...
l_index, l_date, l_time, l_remoteIP and l_page are the fields. They're basically self explanatory.
I would like 6 different tables on the page, Home, Images, About, Downloads, Other and Admin. There should be columns to accommodate each field. Each table should display certain results...
Home - where l_page = fogarty4.co.cc/
Images - where l_page = fogarty4.co.cc/images
About - where l_page = fogarty4.co.cc/about
Downloads - where l_page = fogarty4.co.cc/downloads
Other - where l_page = anything that does not go into a specific table
Admin - where l_remoteIP = [place a spot for me to input [I]2/I] IPs
Also, I would like any visits from those two IPs not to be displayed in any other table.
That's about it. I don't know how clear I made myself, so please reply if you don't understand something.
Thanks in advance,

bluewalrus
03-10-2009, 10:45 PM
Here's an ineffienct and slop-a-ly coded verison without a database...
You need a hit.txt and log.txt for this with permissions to read and write.
This also displays the counter. In hit.txt put in 0 or however many hits you already have. In log .txt put in <table>
<tr class="row"><td class="wider">Visitor ID</td><td>Time</td><td>Date</td><td class="wider">IP Address</td><td class="wider2">Computer Settings</td></tr></table>.

I gave .even and .odd inverse background and font colors to be able to tell difference. The new month isn't working yet though...

<?php
$count = "hit.txt";
$read = fopen($count, 'r');
$rad = fread($read, 99999999999);
fclose($read);
$value = fopen($count, "w+");
$entervalue = $rad + 1;
fwrite($value, $entervalue);
echo "<div id=\"counter\">" . $entervalue . "</div><div id=\"outercount\"></div>";
fclose($value);
?>

<?php
$user = $_SERVER['REMOTE_ADDR'];
$computer = $_SERVER["HTTP_USER_AGENT"];
$info ="log.txt";
$getit = fopen($info, 'r');
$red = fread($getit, 99999999999);
fclose($getit);
$date1 = date('H:i');
$date2 = date('m.j.y');
function is_even($int) {
if ( !is_int($int) ) {
trigger_error('The is_even() function only accepts integers.', E_USER_WARNING);
return;
// Or get rid of trigger_error(xxx) and replace it with: return false;
// to accept any input. This will cause odd numbers and decimals
// to return false.

}

$result = $int % 2 == 0 ? true : false;

return $result;

}
$int = (int) date('j');
if ( is_even($int) ) {
$first = '<tr class="even"><td>';
} else {
$first = '<tr class="odd"><td>';
}
$newmonth = date('m.d H:i');
if ($newmonth == "1/31 23:59" || $newmonth == "2.28 23:59" || $newmonth == "2.29 23:59" || $newmonth == "3.31 23:59" || $newmonth == "4.30 23:59" || $newmonth == "5/31 23:59" || $newmonth == "6/30 23:59" || $newmonth == "7/31 23:59" || $newmonth == "8/30 23:59" || $newmonth == "9/30 23:59" || $newmonth == "10/31 23:59" || $newmonth == "11/30 23:59" || $newmonth == "12/31 23:59") {
$who = "<tr><td class=\"row\" colspan=\"5\"> NEW MONTH</td></tr>" . $first . $entervalue . "</td><td>" . $date1 . "</td><td>" . $date2 . "</td><td>" . $user . "</td><td>" . $computer . "</td></tr>\n</table>\n";
} else {
$who = $first . $entervalue . "</td><td>" . $date1 . "</td><td>" . $date2 . "</td><td>" . $user . "</td><td>" . $computer . "</td></tr>\n</table>";
}
$where = str_replace("</table>", $who, $red);
$logger = fopen($info, "w+");
fwrite($logger, $where);
fclose($logger);
?>
maybe this helps?

bjf98
03-12-2009, 08:50 PM
Thank-you, this may help.
The last part, where do I put the 2 blocks of php code? Maybe I skipped something but do they go into a php file? Or in the page I want to track? Do both blocks of code go into the same place?

bluewalrus
03-13-2009, 12:35 AM
The first block is the counter and must be before the logging. The second block is the tracking info or whatever you want to call it. If you want it on more than one page and want different logs just make multiple text files like log2.txt and hit2.txt and so on... Yes they go into a php file whatever page you want it to be change it to a php and put those in it.

bjf98
03-13-2009, 08:35 PM
Hi, I get this error:


Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 1215752192 bytes) in ****/*****/***/wp-content/themes/k2/footer.php on line 22

bjf98
03-15-2009, 11:24 AM
Okay, I now have a counter up and working, thanks.