dimanche 30 octobre 2016

Bug in my php code which reads data from file and keeps log

I'm writing some php code and I want to compare an ip address with the ip address the user is coming from. This is the code:

<!DOCTYPE html>

<html>
<?php
// set date and hour
$date=date('H:i:s d-m-Y');
// set ip user
$ip=$_SERVER['REMOTE_ADDR'];
// log text
$ipLog="User entered at " . $date . " from ip address: " . $ip . ".\n";
// write log
$firstLogFile="../../logs/firstLogIP.txt";
// ipcheck
$myfile=fopen("../../logs/ownip.txt", "r") or die("Unable to read ownip file!");
$ownip=fgets($myfile);
fclose($myfile);
if($ip!=$ownip) {
    $handleFirstLog=fopen($firstLogFile, 'a') or die("<br><br>Can't open logfile!<br><br>"); 
    fwrite($handleFirstLog, $ipLog);
    fclose($handleFirstLog);
}
?>

<head>
</head>

<body>
    <div class="container">
        <div class="inner-container">
        <div>
            <?php           
            if($ip!=$ownip) {
                print "ips not equal: " . $ip . " " . $ownip;
            } else {
                print "ips equal";
            }
            ?>
        </div>
        </div>
    </div>
<body>

</html>

If the ip address of the user don't equals that of the ip address that's stored in my file (../../logs/ownip.txt) then this should be written to logfile (firstLogIP.txt) and you should see "ips not equal" on the screen.

So if the ip's equal, no log should be written and you should see "ips equal" on the screen.

Althoug the ip address of the user and that of $_SERVER['REMOTE_ADDR'] ARE equal, the script still say's "ips not equal" and the ip address is logged!

Any help with this bug? Thx!!




Aucun commentaire:

Enregistrer un commentaire