Results 1 to 14 of 14

Thread: Php Issue

  1. #1
    Join Date
    May 2006
    Posts
    51

    Question Php Issue

    I don't know whats wrong, but once I moved my scripts from php version 4.4.2 to 5.1.2, this part of my coding seems to be having issues... Could someone take a look at it and tell me whats wrong? Whenever it prints the $line, it doesn't print the ip address...

    $counter = "statistics.txt";
    $today = getdate();
    $month = $today[month];
    $mday = $today[mday];
    $year = $today[year];
    $current_date = $mday . $month . $year;
    $fp = fopen($counter, "a");
    $line = $REMOTE_ADDR . "|" . $mday . $month . $year . "\n";
    $size = strlen($line);
    fputs($fp, $line, $size);
    fclose($fp);

    Thanks in advance...

  2. #2
    Join Date
    Mar 2006
    Posts
    965
    Change:

    PHP Code:
    $line $REMOTE_ADDR "|" $mday $month $year "\n"
    for:

    PHP Code:
    $get_ip = (!empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : ((!empty($_ENV['REMOTE_ADDR'])) ? $_ENV['REMOTE_ADDR'] : getenv("REMOTE_ADDR"));
    $get_ip preg_replace("/[^\.0-9]+/"""$get_ip);
    substr($get_ip050);
    $line = (isset($get_ip)) ? $get_ip """|" $mday $month $year "\n"
    Last edited by horizon; 11-17-2006 at 07:47 PM.

  3. #3
    Join Date
    May 2006
    Posts
    51
    I tried that... but then it doesn't print out the date... line ends up printing just the ip address. Thank you though... that got me closer... I think I can get it from here... thanks
    Last edited by chenster000; 11-17-2006 at 11:43 PM.

  4. #4
    Join Date
    Oct 2004
    Location
    Brisbane, Australia
    Posts
    259
    date('d m y'); maybe?

  5. #5
    Join Date
    Feb 2003
    Location
    L.A. C.A.
    Posts
    335
    No offence to horizon, but his regular expressions and such are unneeded for this type of script and will slightly slow down the script.

    Your script can be solved in an easier to understand way:

    PHP Code:
    $counter "statistics.txt";
    $ip $_SERVER['REMOTE_ADDR'];
    $current_date date('j.n.Y');
    // j is day, 1 to 31. n is month, 1 to 12. Y is year, 1999 etc.
    // More examples: http://php.net/date
    $fp fopen($counter"a");
    $line $ip "|" $current_date "\n";
    $size strlen($line);
    fputs($fp$line$size);
    fclose($fp); 
    Also, as no one mentioned, the reason your script did not work on the new PHP 5 is that register globals was turned off for security reasons.

  6. #6
    Join Date
    Mar 2006
    Posts
    965
    A little verification wouldn't be bad:

    PHP Code:
    $ip $_SERVER['REMOTE_ADDR']; 
    for:

    PHP Code:
    $ip = (!empty($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : ""

  7. #7
    Join Date
    Nov 2003
    Posts
    682
    That doesn't make sense. $_SERVER['REMOTE_ADDR'] is never empty by default, and all your code does is set it to empty if it's already empty.

  8. #8
    Join Date
    Mar 2006
    Posts
    965
    all your code does is set it to empty if it's already empty
    Incorrect. What my code does is checking if it's 'not' empty. If it's not, the IP Address is being called. If it is empty, it will not call any command.

    Althought, what I should of thought is this:

    PHP Code:
    $ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : getenv('REMOTE_ADDR'); 
    The reason why it's being checked is for low server configuration that does not allow 'all usage' of $_SERVER variable (due to some restrictions that might be added). In this case, it will call the getenv function in case it fails.

  9. #9
    I'm curious - what's "low server configuration" and what's "all usage of $_SERVER variable" or are you just making stuff up because of lack of knowledge? Just stop giving away wrong advices and posting unnecessary code..if you can't learn adequate programming practices, don't enforce other people to use them. Thank you for understanding.
    Dyslexics Have More Fnu

  10. #10
    Join Date
    May 2004
    Location
    Lansing, MI, USA
    Posts
    1,548
    Well, if you're going all out, don't forget to check $_SERVER['HTTP_X_FORWARDED_FOR']
    Jacob - WebOnce Technologies - 30 Day 100% Satisfaction Guarantee - Over 5 Years Going Strong!
    Website Hosting, PHP4&5, RoR, MySQL 5.0, Reseller Hosting, Development, and Designs
    Powered By JAM - Professional Website Development - PHP, MySQL, JavaScript, AJAX - Projects Small & Large

  11. #11
    Join Date
    Nov 2003
    Posts
    682
    To clarify-- X_FORWARDED_FOR can be forged, so it shouldn't be used instead of REMOTE_ADDR.
    I'm curious - what's "low server configuration" and what's "all usage of $_SERVER variable" or are you just making stuff up because of lack of knowledge?
    The ternary operator looks confusing-- you have to post code with it to impress people
    Last edited by brendandonhu; 11-18-2006 at 05:36 PM.

  12. #12
    Join Date
    Mar 2006
    Posts
    965
    The ternary operator looks confusing-- you have to post code with it to impress people
    This version is a whole lot better. I thought I had to ignore another of Maximizer's critism. Thanks for your input on this.

  13. #13
    Join Date
    Feb 2003
    Location
    L.A. C.A.
    Posts
    335
    horizon, don't want to put your code down, if his configuration doesn't allow/assign variables to the $_SERVER configuration: thats another problem. Why even bother getting it to check (using valuable load time) when its almost always going to exist.

    This is unneeded code.

  14. #14
    Join Date
    Mar 2006
    Posts
    965
    Explained above.

Posting Permissions

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