我的php代码中有一个Bug,它从文件中读取数据并保存日志

我的php代码中有一个Bug,它从文件中读取数据并保存日志,php,web,Php,Web,我正在编写一些php代码,我想将一个ip地址与用户来自的ip地址进行比较。代码如下: <!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: " .

我正在编写一些php代码,我想将一个ip地址与用户来自的ip地址进行比较。代码如下:

<!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($ip!=$ownip)
{

相反。 说明:在一些脚本语言(如php和javascript)中,有两种字符串比较方法:
=
==
。 第一个是一个快速指针比较字符串,它在大多数情况下都是无用的,它并不真正比较字符串的内容,而是仅当两个字符串来自同一个源时才起作用。
您最好将所有的
==
替换为
==
,以进行字符串比较,这正是您所期望的。如果您需要更强大的功能,您还可以使用
strcmp()

因此,也可以尝试
if(trim($ip)!==trim($ownip)){
你对双等号和三等号的解释是完全错误的。双等号比较值,而三等号比较值和类型,例如
1=='1'
true
1=='1'
false
@kiwi:trim()作业:)您可以将var_dump添加到代码中以打印两个变量的值吗?