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
Old 05-29-2010, 09:33 AM help with log.txt
ZoC
Skilled Talker

Posts: 68
Trades: 0
Hello there, this is the code

PHP Code:
<?php

 $iplogfile 
"log.txt";
 
$fp fopen($iplogfile"a");

 if (
$fp)
 {
  
fputs($fp$_SERVER["REMOTE_ADDR"]." \n");
  
fclose($fp);
 }

?>
Can someone help me how i can make if the $_SERVER["REMOTE_ADDR"] is aready on log.txt to dont add again the ip on log.txt

Thanks
ZoC is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 05-29-2010, 02:07 PM Re: help with log.txt
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
PHP Code:
$already_logged false//Flag to determine if record already in log

$iplogfile "log.txt";

//Open log to search for remote address
$handle fopen($iplogfile"r");
if (
$handle) {
    while (!
feof($handle)) {
        
$buffer trim(fgets($handle4096));
        if (
$buffer == $_SERVER["REMOTE_ADDR"]) {
          
//Remote address found, so update flag and stop looping through file contents
          
$already_logged true;
          break;
        }
    }
    
fclose($handle);
}

if (!
$already_logged) {
  
//Entry not in log, so add
  
$fp fopen($iplogfile"a");

  if (
$fp) {
    
fwrite($fp$_SERVER["REMOTE_ADDR"]." \n");
    
fclose($fp);
  }

But, I don't recommend that you use that. It'd be more efficient, requiring fewer file reads, to just add the record to the file and then clean up duplicates later -- perhaps using a session variable to track if it had already been added during that session.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-30-2010, 07:19 AM Re: help with log.txt
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
$cmd = "grep '{$ip}' '{$logfile}' || echo '{$ip}' >> '{$logfile}'";
exec($cmd);

Use the powah of teh leenoox!!!!
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!

Last edited by mtishetsky; 05-30-2010 at 07:19 AM.. Reason: Use the powah of teh leenoox!!!!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to help with log.txt
 

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.12155 seconds with 12 queries