PHP如何在插入数据之前在file.txt上搜索重复项

时间:2014-11-15 19:34:40

标签: php file duplicates

基本上,我有一个我编写的代码,它与HTML表单相关联。 当用户提交表单时,我的php代码已经检查错误并验证它,然后将all插入到同一目录下的file.txt中,一切正常。

我现在需要做什么,而且我不知道如何使用php在将数据插入文件之前检查文件是否重复,这样如果它是重复的,它会给出错误用户,不会插入文件。

我到处寻找这个,但我似乎唯一能找到的是如何用MySQL做这个,我需要用.txt文件做这个,有可能吗?

这是我目前的代码:

    <?php   $username = ""; $ip = ""; $port = ""; $usernameErr = ""; $ipErr = ""; $portErr = ""; $readytosend1 = ""; $readytosend2 = ""; $readytosend3 = "";?>
    <!DOCTYPE html>
    <html>
    <head>
     <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>
    <?php
    if (!isset($_POST["submit"])) {

    } else {

    function test_input($data)
    {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

      if (empty($_POST["username"])) {
        $usernameErr = "This field is required";
        $readytosend1 = "no";
      } else {
        $username = test_input($_POST["username"]);
        $readytosend1 = "yes";
      }
    }


    if ($_SERVER["REQUEST_METHOD"] == "POST") {

      if (empty($_POST["ip"])) {
        $ipErr = "This field is required";
        $readytosend2 = "no";
      } else {
        $ip = test_input($_POST["ip"]);
        if(!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)){
            $ipErr = "The ip you entered is invalid";
            $readytosend2 = "no";
        } else {
            $ipErr = "";
            $readytosend2 = "yes";
        }
      }
    }

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

      if (empty($_POST["port"])) {
        $portErr = "This field is required";
        $readytosend3 = "no";
      } else {
        $port = test_input($_POST["port"]);
        if($port > 1024 && $port < 65535){
            $portErr = "";
            $readytosend3 = "yes";
        } else {
            $portErr = "The port you entered is invalid";
            $readytosend3 = "no";
        }
      }
    }
    if($readytosend1 == "yes" && $readytosend2 == "yes" && $readytosend3 == "yes"){ 
    $file = fopen("file.txt", "a") or die("Unable to open file!");
    $txt = $username ." : ". $ip ." : ". $port ."\n\n";
    fwrite($file, $txt);
    fclose($file);
    }
    }
    ?> 

    <div align="center">
        <form id="orderform" method="post">
            <input type="text" value="" style="margin-top:5px;" placeholder="Username" name="username"><br><span id="error1" class="error"><?php echo $usernameErr;?></span><br>
            <input type="text" value="" style="margin-top:5px;" placeholder="IP" name="ip"><br><span id="error2" class="error"><?php echo $ipErr;?></span><br>
            <input type="text" value="" style="margin-top:5px;margin-bottom:10px;" placeholder="Port" name="port"><br><span id="error3" class="error"><?php echo $portErr;?>        </span><br>
            <input id="submit" type="submit" name="submit" value="Submit">  
        </form>
    </div>

PS:使用MySQL不是一种选择。

感谢阅读。

2 个答案:

答案 0 :(得分:2)

试试这个:

if($readytosend1 == "yes" && $readytosend2 == "yes" && $readytosend3 == "yes"){
    //exist check & insert part 
        $fd=@explode("\n",@file_get_contents("data.txt"));   // read file
        $txt = $username ." : ". $ip ." : ". $port ;
        if(!in_array($txt,$fd)){
            $fd[]=$txt;
            @file_put_contents("data.txt",@implode("\n",$fd));  //  insert
        }
        else{
            echo "data exist";
        }   
}

答案 1 :(得分:1)

您必须打开文件并逐行浏览。根据您要查找的字符串检查每一行。如果你找到它,标记并突破循环。然后,您可以写入文件,也可以显示错误消息。

    if($readytosend1 == "yes" && $readytosend2 == "yes" && $readytosend3 == "yes"){
        $txt = $username ." : ". $ip ." : ". $port ."\n\n";
        $lines = file('file.txt');
        $match = false;

        foreach ($lines as $line_num => $line) {
            if($line === $txt) {
                $match = true;
                break;
            }
        }

        if(!$match) {
            $file = fopen("file.txt", "a") or die("Unable to open file!");
            fwrite($file, $txt);
            fclose($file);
        } else {
            // record already exists, do something with an error message
        }
    }
    }
    ?>