输出不显示重新加载页面的时间

时间:2014-06-04 12:45:08

标签: php file

我创建了一个小应用程序,它将$email变量的内容写入文件mailadressen.txt。如果文件存在,则会显示消息“电子邮件地址已存在”(电子邮件地址bereits vorhanden)。如果我更改邮件地址并重新加载页面,则它不会输出任何内容。但是,如果我再次使用新的电子邮件地址重新加载它,如果再次显示消息“电子邮件地址已存在”。

有人可以给我一个提示,为什么它在第一次重装时没有输出任何内容但只在第二次重装时输出?

<?php
    $email = "Kevin@duck.ente";

    // open file and read & write
    $handle = fopen ("mailadressen.txt", "a+");

    while ( $inhalt = fgets ($handle, 4096 ))
    {
      $inhalt = trim ( $inhalt );
      echo "<li> |". $inhalt ."| </li>";
      if ( $inhalt == $email)
      {
        echo "E-Mail-Adresse bereits vorhanden";
        continue;
      }
    }

    fwrite($handle, $email);

    // new line
    fwrite($handle, "\r\n");

    fclose($handle);
    ?>

1 个答案:

答案 0 :(得分:1)

根据我的评论,这是一个解决方案。

$email = "Kevin@duck.ente";

// open file and read & write
$handle = fopen ("mailadressen.txt", "a+");

while ( $inhalt = fgets ($handle, 4096 )){

  $inhalt = trim ( $inhalt );
  echo "<li> |". $inhalt ."| </li>";

  if ( $inhalt == $email){
    $email_exists = true;
    $msg = "E-Mail-Adresse bereits vorhanden";
    continue;
  }
}

//echo the message if email already exists
if(isset($email_exists) && $email_exists === true){
  echo $msg;
}
  else{
    //let's write only non existing email to the file
    fwrite($handle, $email);

    // new line
    fwrite($handle, "\r\n");

    echo "Wrote new email: " . $email . " into the mailadressen.txt file.";
  }

fclose($handle);