Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: IP Logger

  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    IP Logger

    i'm looking for a simple PHP script that when someone goes to it, it will log the users IP and Time in a log.txt file. i looked around this board and other PHP scripting sites and havent found anything.

    thanks,
    mike

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    PHP Code:
    <?php 
    $file_name 
    "accesslog.txt"
    $data $REMOTE_ADDR.",".date("l, d-M-Y H:i:s T")."\n";

    $file_pointer fopen($file_name"a"); 
    $lock flock($file_pointerLOCK_EX); 

    if (
    $lock){
     
    fputs($file_pointer$data); 
     
    flock($file_pointerLOCK_UN); 

    fclose($file_pointer); 
    ?>
    enjoy
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Oct 2002
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts
    your are awesome!
    thanks for your help

    mike

  • #4
    Regular Coder Feyd's Avatar
    Join Date
    May 2002
    Location
    Los Angeles, CA Maxim: Subvert Society
    Posts
    403
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Spookster, oh you wonderful and almighty and all-powerful PHP god! Could you please be so kind if it is not too much trouble your royalness if you could not use double quotes when you don't need them, you're wasting your valuable time...if the string/variable does not need to be parsed for PHP rendered items, then you should enclose it in single quotes since PHP knows that single quotes means "nothing to modify in this string, move along" where double quotes would tell PHP to look for anything in this string which could be modified and then modify it...thus, if there is nothing to modify in that string, and you are still looking for it, then you are wasting your precious time when you could be doing many other wonderful things like giving me lessons on how to program in PHP.

    So slightly updated code would look like:
    PHP Code:
    <?php 
        $file_name 
    'accesslog.txt';
        
    $data $_SERVER['REMOTE_ADDR'] . ',' date('l, d-M-Y H:i:s T') . "\n";
        
        
    $file_pointer fopen($file_name'a');
        
    $lock flock($file_pointerLOCK_EX);
        
        if (
    $lock) {
            
    fputs($file_pointer$data);
            
    flock($file_pointerLOCK_UN);
        }
        
    fclose($file_pointer);
    ?>
    Notice only the newline command has double quotes, and that is so that it will be parsed as a newline command, rather than just the text output.
    Last edited by Spookster; 02-13-2003 at 12:57 AM.
    Moderator, Perl/CGI Forum
    shadowstorm.net - subvert society

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    "Premature optimization is the root of all evil."

    Have you any benchmarks concerning the use of double quoted strings opposed to single quoted ones? And, how often are they really the bottleneck that eats away system resources? In spooksters code I'd rather say the time-consuming operation will be the writing to the file, and I highly doubt that the time gain of your optimization will be more than some microseconds...

    *ducks for cover*

  • #6
    Regular Coder Feyd's Avatar
    Join Date
    May 2002
    Location
    Los Angeles, CA Maxim: Subvert Society
    Posts
    403
    Thanks
    0
    Thanked 0 Times in 0 Posts
    True, it isn't that noticeable in a human measure of time, but it can be ages in machine time. Besides, I typically deal in huge volumes of code, thousands of lines...small little lags add up, greatly.

    Regardless of that, it is best practice to code efficiently...no matter what it is you are coding. I'm not saying go back and re-write all your scripts...more along the lines of, in the future, make it easier on yourself by a few slight alterations.
    Moderator, Perl/CGI Forum
    shadowstorm.net - subvert society

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I actually do use single quotes in my php scripts. And yes, the scripts I deal with can get quite large, comparable to yours. Though then it's more likely to be a collection of objects and the real problems with execution time come not from double quoted strings, but that's not my point.

    For me, this single quote issue is one of those little tricks that are close to "premature optimization". Do you perform mathematical calculations solely with bitshifting? Do you store preferences in bitfields or do you consider writing a container class for it? Both approaches may have their justifications, and neither is the perfect one. It depends on what you want to achieve - if your application is in dire need of a lightning-fast execution time, then optimize where you can, but the goal might be differently set. Like developing an easy to use application framework.

    Last but not least, a great advantage of using single quoted strings is that within those string HTML attributes can be surrounded by double quotes and left so in your code untouched. Makes it easier for me to incorporate quickly some changes in the markup I get from the designer.

  • #8
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by Feyd
    Spookster, oh you wonderful and almighty and all-powerful PHP god! Could you please be so kind if it is not too much trouble your royalness if you could not use double quotes when you don't need them, you're wasting your valuable time...if the string/variable does not need to be parsed for PHP rendered items, then you should enclose it in single quotes since PHP knows that single quotes means "nothing to modify in this string, move along" where double quotes would tell PHP to look for anything in this string which could be modified and then modify it...thus, if there is nothing to modify in that string, and you are still looking for it, then you are wasting your precious time when you could be doing many other wonderful things like giving me lessons on how to program in PHP.

    So slightly updated code would look like:
    PHP Code:
    <?php 
        $file_name 
    'accesslog.txt';
        
    $data $_SERVER['REMOTE_ADDR'] . ',' date('l, d-M-Y H:i:s T') . "\n";
        
        
    $file_pointer fopen($file_name'a');
        
    $lock flock($file_pointerLOCK_EX);
        
        if (
    $lock) {
            
    fputs($file_pointer$data);
            
    flock($file_pointerLOCK_UN);
        }
        
    fclose($file_pointer);
    ?>
    Notice only the newline command has double quotes, and that is so that it will be parsed as a newline command, rather than just the text output.
    lol Yeah, come in my forum and talk smack and see what happens. Bow down before me swamp slime.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #9
    Regular Coder Feyd's Avatar
    Join Date
    May 2002
    Location
    Los Angeles, CA Maxim: Subvert Society
    Posts
    403
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmmm...no?

    I absolutely love your edits, by the way....you RAMF
    Moderator, Perl/CGI Forum
    shadowstorm.net - subvert society


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •