• RSS
  • Twitter
  • FaceBook

Security Forums

Log in

FAQ | Search | Usergroups | Profile | Register | RSS | Posting Guidelines | Recent Posts

PHP IP Logging

Users browsing this topic:0 Security Fans, 0 Stealth Security Fans
Registered Security Fans: None
Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More

View previous topic :: View next topic  
Author Message
TheKingster
Link Spammer
Link Spammer


Joined: 03 May 2002
Posts: 0
Location: UK

Offline

PostPosted: Thu Feb 27, 2003 1:37 am    Post subject: PHP IP Logging Reply with quote

I have some code for a counter for a website, however I only want to increment the counter if its from an ip that hasn't already visited the site. So I need to log the ip to a file, when someone visits, firstly checking if the ip doesn't already exist.

Is this the standard way counters work? Or is there another way? I can't imagine you can just hit F5 to increment a counter on other sites.

I don't want people to be able to keep pressing F5 and watch the number fly up, I want it to be more realistic than that. I also don't want to use a 3rd party stats provider, the webserver is in my house and i want to run it all from there. At the moment the PHP just increments the number in a file.

In fact, probably the same ip increments the number if the visits are on a different date or something.

Any ideas?
Back to top
View user's profile Send private message
ShaolinTiger
Forum Fanatic
Forum Fanatic


Joined: 18 Apr 2002
Posts: 16777215
Location: Kuala Lumpur, Malaysia

Offline

PostPosted: Thu Feb 27, 2003 1:38 am    Post subject: Reply with quote

Use sessions.

You can work out the rest yourself Smile
Back to top
View user's profile Send private message Visit poster's website
myhatisred
Just Arrived
Just Arrived


Joined: 11 Jan 2003
Posts: 0


Offline

PostPosted: Thu Feb 27, 2003 1:47 am    Post subject: Reply with quote

here u go:

http://php.auburn.edu/helpdesk/php/count_tokens.txt
Back to top
View user's profile Send private message Visit poster's website AIM Address
myhatisred
Just Arrived
Just Arrived


Joined: 11 Jan 2003
Posts: 0


Offline

PostPosted: Thu Feb 27, 2003 1:51 am    Post subject: Reply with quote

oh, one more thing, to use that script, you need to create 2 files called ip_data.txt and count_data.txt and put "0" in each one.
Back to top
View user's profile Send private message Visit poster's website AIM Address
TheKingster
Link Spammer
Link Spammer


Joined: 03 May 2002
Posts: 0
Location: UK

Offline

PostPosted: Thu Feb 27, 2003 12:09 pm    Post subject: Reply with quote

Well Im having a go now, i'll let you know how I get on.
Back to top
View user's profile Send private message
TheKingster
Link Spammer
Link Spammer


Joined: 03 May 2002
Posts: 0
Location: UK

Offline

PostPosted: Thu Feb 27, 2003 12:29 pm    Post subject: Reply with quote

Just added it, simple copy and paste and worked first time.

I might put a batch file on the server to replace the ip log file with an empty one each day, so that the same ip on a different day still increments the count, as it is a seperate visit.

Thing is if your behind a firewall, visits from the same network will only result in the visitor count going up once. Is this the same with all counters?
Back to top
View user's profile Send private message
ShaolinTiger
Forum Fanatic
Forum Fanatic


Joined: 18 Apr 2002
Posts: 16777215
Location: Kuala Lumpur, Malaysia

Offline

PostPosted: Thu Feb 27, 2003 12:31 pm    Post subject: Reply with quote

No, most use sessions or cookies, usually a combination of both (like this site), as we both said above..probably 10 hour session time or something
Back to top
View user's profile Send private message Visit poster's website
Giro
New Member
New Member


Joined: 25 Mar 2004
Posts: 22
Location: England

Offline

PostPosted: Thu Feb 27, 2003 12:47 pm    Post subject: Reply with quote

Im no php guru but couldnt that log get corrupt because it doesnt lock the file when opening it. Please correct me if im wrong.
Back to top
View user's profile Send private message
TheKingster
Link Spammer
Link Spammer


Joined: 03 May 2002
Posts: 0
Location: UK

Offline

PostPosted: Thu Feb 27, 2003 12:57 pm    Post subject: Reply with quote

myhatisred wrote:
oh, one more thing, to use that script, you need to create 2 files called ip_data.txt and count_data.txt and put "0" in each one.


Actually, its not working. I have put a 0 in both files, but they are staying at 0, no change when different ips visit the site.

Do I need to initialise the ip log file in a different way?
Back to top
View user's profile Send private message
ThePsyko
SF Mod
SF Mod


Joined: 17 Oct 2002
Posts: 16777178
Location: California

Offline

PostPosted: Thu Feb 27, 2003 5:58 pm    Post subject: Reply with quote

I use mysql and a 2 hour window on mine:
Code:

   $check = mysql_query( "select * from $ltable where ip='$ip' AND d='$day' AND t >'$window'" )
      or die( "query died with error $mysql_errno() : $mysql_error()" );

   if( $row = mysql_fetch_array( $check ))
   {

   // UPDATE TABLE ROW FOR IP (RETURN VISIT)
      $recno = $row[id];
      $last = $row[lastaction];
      $spent = ($t - $last);


      $newpages .= $row[pages] ." ($spent),$page";

      if(( !$row[fpage]) && (!eregi("index", $page )))
      {
         $mod = mysql_query( "update $ltable set t='$t', pages='$newpages', fpage='$page', lastaction='$t' where id='$recno'" );

      } else {

         $mod = mysql_query( "update $ltable set t='$t', pages='$newpages', lastaction='$t' where id='$recno'" );
      }

   } else {

   // ADD NEW ENTRY TO LOG
      parse_agent( $agent );

   // MAKE OS A SESSION VARIABLE
      if( !isset( $_SESSION[os] ))
         $_SESSION[os] = $os;

      if( eregi( "index", $page ))
      {
         $query = "insert into $ltable( t, d, ip, host, os, brow, pages, referer, arrived, lastaction ) values( '$t', '$day', '$ip', '$host', '$os', '$browser', '$page', '$referer', '$t', '$t' )";

      } else {

         $query = "insert into $ltable( t, d, ip, host, os, brow, pages, fpage, referer, arrived, lastaction ) values( '$t', '$day', '$ip', '$host', '$os', '$browser', '$page', '$page', '$referer', '$t', '$t' )";
      }

      $result = mysql_query( $query );

   }



That allows me to pull from the table based on different criteria.. it also allows me to track which page they looked at first other than the index page Smile
Back to top
View user's profile Send private message Send e-mail
TheKingster
Link Spammer
Link Spammer


Joined: 03 May 2002
Posts: 0
Location: UK

Offline

PostPosted: Thu Feb 27, 2003 6:47 pm    Post subject: Reply with quote

Thats a bit too much for what I want. I want a simply counter function that uses sessions so when they revisit it still increments the counter.
Back to top
View user's profile Send private message
Battery Powered
Just Arrived
Just Arrived


Joined: 10 Apr 2003
Posts: 0


Offline

PostPosted: Fri Apr 11, 2003 12:07 am    Post subject: Reply with quote

Ok if you want something just PHP, no databasing etc... try the following:

Code:
<?php
$timer = 25;
$filename = "online.txt";
if (!$datei) $datei = dirname(__FILE__)."/$filename";
$time = @time();
$ip = $REMOTE_ADDR;
$string = "$ip|$time\n";
$a = fopen("$filename", "a+");
fputs($a, $string);
fclose($a);

$timeout = time()-(60*$timer);

$all = "";
$i = 0;
$datei = file($filename);
for ($num = 0; $num < count($datei); $num++) {
   $pieces = explode("|",$datei[$num]);

      if ($pieces[1] > $timeout) {
         $all .= $pieces[0];
         $all .= ",";
      }
   $i++;
}

$all = substr($all,0,strlen($all)-1);
$arraypieces = explode(",",$all);
$useronline = count(array_flip(array_flip($arraypieces)));
$dell = "";
for ($numm = 0; $numm < count($datei); $numm++) {
   $tiles = explode("|",$datei[$numm]);
      if ($tiles[1] > $timeout) {
         $dell .= "$tiles[0]|$tiles[1]";
      }
}

if (!$datei) $datei = dirname(__FILE__)."/$filename";
$time = @time();
$ip = $REMOTE_ADDR;
$string = "$dell";
$a = fopen("$filename", "w+");
fputs($a, $string);
fclose($a);
?>


Save that as 'users_online.php4' or any name you prefer, then in the page you want to include the stats put:

Code:
<?php include("users_online.php4"); ?>


That will include the counting script, then where ever you wish to print the count put this code:
Code:
<?php echo $useronline; ?>


And your problem should be solved, let me know if i went too fast j/k : )

(btw - CHMOD 666 to the logfile name, eg 'online.txt' as shown above, and its a good idea to leave this file out of the webroot)

All the best,
B.P
Back to top
View user's profile Send private message
Display posts from previous:   

Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More All times are GMT + 2 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Community Area

Log in | Register