title: PHP keep record of unsuccessful logins
body: <p>How can I keep a record of the unsuccessful logins to my website? I would like the users browser, IP address, the username that was entered to attempt to login with, and the date and time the login was attempted, all to be stored in a text file. Although this would be easier in a database, I would like it in a text file.</p>
  
  <p>Here is my code:
  </p>
  
  <pre><code>&lt;head&gt;
  &lt;title&gt;Landing page&lt;/title&gt;
  &lt;link rel="stylesheet" type="text/css" href="css.css"&gt;
      &lt;div class="loginform"&gt;
          &lt;form method="post"&gt;
              &lt;input type ='text' name="username"&gt;
              &lt;input type ='text' name="password"&gt;
              &lt;input type ='submit' name="submit"&gt;
          &lt;/form&gt;
      &lt;/div&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;?php
  
  
  if (isset($_POST['submit'])) 
  {
  
  
  $conn = new PDO("mysql:host=localhost;dbname=user_login",'root','');
  $name = $_POST["username"];
  $pass = $_POST["password"];
  $sql = "SELECT * FROM users WHERE Username = ? AND Password  = ?";
  $q = $conn-&gt;prepare($sql);
  $q-&gt;execute(array($name,$pass));
  $count = $q-&gt;rowCount();
  if ($count==1) 
  {
  session_start();
        $_SESSION["logged_in"] = "YES";
        echo "&lt;h1&gt;You are now logged in&lt;/h1&gt;";
        echo "&lt;p&gt;&lt;a href='secure1.php'&gt;Link to protected file&lt;/a&gt;&lt;/p&gt;";
        echo "&lt;p&gt;&lt;a href='secure2.php'&gt;Link to protected file #2&lt;/a&gt;&lt;/p&gt;";
        echo "&lt;p&gt;&lt;a href='public.html'&gt;Link to public page&lt;/a&gt;&lt;/p&gt;";
  $q-&gt;setFetchMode(PDO::FETCH_BOTH);
  while($row = $q-&gt;fetch())
  {
  echo '&lt;p&gt;Welcome &lt;b&gt;'.$row['Firstname'].$row['Lastname'].'&lt;/b&gt;&lt;br&gt;&lt;/p&gt;';     //Just to show you output
  $_SESSION["Firstname"]  = $row['Lastname'];
  }
  echo '&lt;p&gt;&lt;a href="logout.html"&gt;logout&lt;/a&gt;&lt;/p&gt;';
  }
  else   
  {
  session_start();
        $_SESSION["logged_in"] = "NO";
        echo "&lt;h1&gt;You are NOT logged in &lt;/h1&gt;";
        echo "&lt;p&gt;&lt;a href='secure1.php'&gt;Link to protected file&lt;/a&gt;&lt;/p&gt;";
        echo "&lt;p&gt;&lt;a href='secure2.php'&gt;Link to protected file #2&lt;/a&gt;&lt;/p&gt;";
        echo "&lt;p&gt;&lt;a href='public.html'&gt;Link to public page&lt;/a&gt;&lt;/p&gt;";
        echo "&lt;p&gt;Welcome &lt;b&gt;Guest&lt;/b&gt;&lt;/p&gt;";
  }
  }
  ?&gt;
  
  
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>Thanks in advance</p>