Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Need help in php - how to log unique visitors with this code?
Old 03-29-2009, 10:08 PM Need help in php - how to log unique visitors with this code?
Experienced Talker

Posts: 38
Trades: 0
hi guys,

recently i've just moved to a new hosting company. my cgi doesn't work with php pages anymore, the virtual () is not working.

so, i searched the web, got this script and modified it. everything is working the same way as my previous cgi script, except that, it logs everything, even page reload.

the php script:
PHP Code:
<?php
$logfile 
"log.db";
if (
file_exists($logfile)) {
$handle fopen($logfile"r");
$log fread($handlefilesize($logfile));
fclose($handle);
} else {
die (
"The log file doesn't exist!");
}
 
$log explode("\n"trim($log));
for (
$i 0$i count($log); $i++) {
$log[$i] = trim($log[$i]);
$log[$i] = explode('|'$log[$i]);
}
echo 
"Total Entries : " count($log);
echo 
'<style type="text/css">
body {
 background-color: #000;
 color: #ffff80;
 text-align: center;
 font-size: 13px
}
.wrapper {
 width: 90%;
 text-align: center;
 margin: 0 auto;
}
a {
 color: #33ccff;
 text-decoration: underline;
}
a:hover {
 text-decoration: none;
}
table {
 width: 100%;
 border-collapse: collapse;
 border: 2px ridge #ccc;
 text-align: center;
 margin: 0 auto;
 font-size: 13px
}
td {
 background-color: #0f0f4f;
 padding: 3px;
 border: 2px ridge #ccc
}
td.a {
 width: 12%;
}
td.b {
 width: 28%;
}
td.c {
 width: 60%;
}
</style>'
;
echo 
'<div class="wrapper"><table>';
foreach (
$log as $logline) {
 if (
$logline['1'] == '') {
  echo 
'<tr>';
  echo 
'<td colspan="2"><b>' date('D j M Y G:i:s T'$logline['2']) . '</b></td>';
  echo 
'<td class="c">' $logline['5'] . '</td>';
  echo 
'</tr>';
  echo 
'<tr>';
  echo 
'<td class="a"><b>IP Address</b><br />' $logline['0'] . '</td>';
  echo 
'<td class="b"><b>Hostname</b><br />' $logline['4'] . '</td>';
  echo 
'<td class="c"><b>Browser/OS</b><br />' $logline['3'] . '</td>';
  echo 
'</tr>';
 } else {
  echo 
'<tr>';
  echo 
'<td class="a"><a href="' htmlspecialchars(urldecode($logline['1'])) . '"><b>Referer</b></a>';
  echo 
'<td class="b"><b>' date('D j M Y G:i:s T'$logline['2']) . '</b></td>';
  echo 
'<td class="c">' $logline['5'] . '</td>';
  echo 
'</tr>';
  echo 
'<tr>';
  echo 
'<td class="a"><b>IP Address</b><br />' $logline['0'] . '</td>';
  echo 
'<td class="b"><b>Hostname</b><br />' $logline['4'] . '</td>';
  echo 
'<td class="c"><b>Browser/OS</b><br />' $logline['3'] . '</td>';
  echo 
'</tr>';
 }
}
echo 
'</table></div>';
?>
appreciate if someone could help, thanks.
superkingkong is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-30-2009, 11:37 AM Re: Need help in php - how to log unique visitors with this code?
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
Is there any other bits of code, nothing seems to be being written to the file...
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 03-30-2009 at 11:38 AM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 03-30-2009, 05:27 PM Re: Need help in php - how to log unique visitors with this code?
Experienced Talker

Posts: 38
Trades: 0
oops, my mistake. i think last time i was too sleepy and tired... after looking at the code for few hours

the one above is to list the output from the log. below is the one which will capturing the users information

PHP Code:
<?php
// Getting the information
$ipaddress $_SERVER['REMOTE_ADDR'];
$page "http://" $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$referrer $_SERVER['HTTP_REFERER'];
$datetime date('D, M j, Y (T) \a\t h:i:s A');
$useragent $_SERVER['HTTP_USER_AGENT'];
$remotehost = @getHostByAddr($ipaddress);

// Create log line
$logline $ipaddress '|' $referrer '|' $datetime '|' $useragent '|' $remotehost '|' $page "\n";

// Write to log file:
$logfile 'log.db';

if (!
$handle fopen($logfile'r+')) {
    die(
"Failed to open log file");
}

file_put_contents($logfile$logline file_get_contents($logfile));

fclose($handle);

?>

Last edited by superkingkong; 03-30-2009 at 05:31 PM..
superkingkong is offline
Reply With Quote
View Public Profile
 
Old 03-30-2009, 05:34 PM Re: Need help in php - how to log unique visitors with this code?
Experienced Talker

Posts: 38
Trades: 0
this is the perl script, it has something like the time comparison, which i'm not sure in php, i'm still a newbie

Code:
if (-e "$hostfile")
    {
        open(HOST,"$hostfile");
        $hostline = <HOST>;
        chop($hostline) if $hostline =~ /\n$/;
        close(HOST);
        ($old_time,$old_number,$old_page,$old_browser) = split(/\|/,$hostline);
    }

    # save host and time info and check if this is a page reload
    open(HOST,">$hostfile");
    $seconds = time;
    print HOST "$seconds\|$ENV{REMOTE_ADDR}\|$ENV{'DOCUMENT_URI'}\|$ENV{'HTTP_USER_AGENT'}";
    close(HOST);

    if ($time - $old_time < $interval && $ENV{REMOTE_ADDR} eq $old_number && $ENV{'DOCUMENT_URI'} eq $old_page && $ENV{'HTTP_USER_AGENT'} eq $old_browser)
    {
don't do anything
    } else {
process it
}
anyway to convert this to php so that i can incorporate it into the above-mentioned code?

thanks.
superkingkong is offline
Reply With Quote
View Public Profile
 
Old 03-31-2009, 04:43 PM Re: Need help in php - how to log unique visitors with this code?
Experienced Talker

Posts: 38
Trades: 0
hi,

i'm wondering, since php has more functionality, is session going to help?

i found this session code, but not sure about the meaning...

session_start();

$pageRefreshed = false;
if (isset($_SESSION['LAST_REQUEST']) && $_SERVER['REQUEST_URI'] === $_SESSION['LAST_REQUEST']['REQUEST_URI']) {
if (isset($_SERVER['HTTP_REFERER'])) {
// check if the last request’s referrer is the same as the current
$pageRefreshed = $_SERVER['HTTP_REFERER'] === $_SESSION['LAST_REQUEST']['HTTP_REFERER'];
} else {
// check if the last request didn’t have a referrer either
$pageRefreshed = $_SERVER['HTTP_REFERER'] === null;
}
}
// set current request as “last request”
$_SERVER['LAST_REQUEST'] = array(
'REQUEST_URI' => $_SERVER['REQEUST_URI'],
'HTTP_REFERER' => isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null
);

appreciate your feedback
superkingkong is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Need help in php - how to log unique visitors with this code?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.79437 seconds with 12 queries