title: Why doesn't the users IP address output in my text file when they input the wrong login details?
body: <p>I'm having trouble outputting to the to the text file the users IP address when they input the wrong login credentials, in my login form script. How can I also fix this? <a href="https://i.stack.imgur.com/6FlsX.png" rel="nofollow noreferrer">Error at the bottom of the screenshot.</a> GitHub repo <a href="https://github.com/liamdocherty/unit-27-a2-script" rel="nofollow noreferrer">link</a></p>
  
  <p>Moreover, I believe the issue for both of these two problems is in the <code>functions.php</code>. However, I will put the <code>index.php</code> file coding incase.</p>
  
  <p><strong>functions.php</strong>
      
  
  <pre><code>function getBrowser()
  {
      $u_agent = $_SERVER['HTTP_USER_AGENT'];
      $ip_add = $_SERVER['HTTP_CLIENT_IP'];
      $bname = 'Unknown';
      $platform = 'Unknown';
      $version= "";
  
      //Fetch platform info
      if (preg_match('/macintosh|mac os x/i', $u_agent)) {
          $platform = 'mac';        //supports mac
      }
      elseif (preg_match('/windows|win32/i', $u_agent)) {
          $platform = 'windows'; //supports windows
      }
  
      // Next get the name of the useragent yes seperately and for good reason
      if(preg_match('/MSIE/i',$u_agent) &amp;&amp; !preg_match('/Opera/i',$u_agent))
      {
          $bname = 'Internet Explorer'; //supports IE
          $ub = "MSIE";
      }
      elseif(preg_match('/Firefox/i',$u_agent)) //supports FIREFOX
      {
          $bname = 'Mozilla Firefox';
          $ub = "Firefox";
      }
      elseif(preg_match('/Chrome/i',$u_agent))
      {
          $bname = 'Google Chrome'; //supports chrome
          $ub = "Chrome";
      }
      elseif(preg_match('/Safari/i',$u_agent))
      {
          $bname = 'Apple Safari';
          $ub = "Safari"; //supports safari
      }
      elseif(preg_match('/Microsoft edge/i',$u_agent))
      {
          $bname = 'Microsoft edge';
          $ub = "Edge"; //supports Edge
      }
  
  
      // finally get the correct version number
      $known = array('Version', $ub, 'other');
      $pattern = '#(?&lt;browser&gt;' . join('|', $known) .
          ')[/ ]+(?&lt;version&gt;[0-9.|a-zA-Z.]*)#';
      if (!preg_match_all($pattern, $u_agent, $matches)) {
          // we have no matching number just continue
      }
  
      // see how many we have
      $i = count($matches['browser']);
      if ($i != 1) {
          //we will have two since we are not using 'other' argument yet
          //see if version is before or after the name
          if (strripos($u_agent,"Version") &lt; strripos($u_agent,$ub)){
              $version= $matches['version'][0];
          }
          else {
              $version= $matches['version'][1];
          }
      }
      else {
          $version= $matches['version'][0];
      }
  
      // check if we have a number
      if ($version==null || $version=="") {$version="?";}
  
      return array(
          'name'      =&gt; $bname,
          'version'   =&gt; $version,
          'platform'  =&gt; $platform,
          'ip' =&gt; $ip_add,
      );
  }
  
  function saveLog($log, $logFileName = 'log')
  {
      if (is_null($log)) {
          $log = 'empty';
      }
      $now = time();
  
      $logFileName = str_replace('.txt', '', $logFileName);
      $logFileName = __DIR__.'/' . $logFileName .'.txt';
  
      $data = "\n-------";
      $data .= $now;
      $data .= "-----------\n";
      $data .= print_r($log, true);
      $data .= " \n\n------- SCRIPT----------\n\n";
  
      file_put_contents($logFileName, $data, FILE_APPEND);
  
  }
  </code></pre>
  
  <p><strong>index.php code</strong></p>
  
  <pre><code>&lt;?php
  //PHP method to use cache memory to store details
  session_start();
  //Makes the "config.php" file available to be executed from this page
  require_once('dbconfig/config.php');
  require_once 'functions.php';
  ?&gt;
  
      &lt;!DOCTYPE html&gt;
      &lt;html&gt;
  
      &lt;head&gt;
          &lt;!-- Site title, CSS external file and font awesome --&gt;
          &lt;title&gt;Login Page&lt;/title&gt;
          &lt;link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous"&gt;
          &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous"&gt;
          &lt;link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.css"&gt;
          &lt;link rel="stylesheet" type="text/css" href="design.css"&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;!-- Nav Bar--&gt;
          &lt;nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-bottom animated bounceInDown"&gt;
              &lt;div class="container"&gt;
                  &lt;button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"&gt;
                      &lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt;
                  &lt;/button&gt;
                  &lt;div class="collapse navbar-collapse" id="navbarResponsive"&gt;
                      &lt;ul class="navbar-nav ml-auto"&gt;
                          &lt;li class="nav-item active"&gt;
                              &lt;a class="nav-link" href="http://webbrowserinfo.96.lt"&gt;Go back to Homepage &lt;i class="fa fa-home"&gt;&lt;/i&gt;
                  &lt;span class="sr-only"&gt;(current)&lt;/span&gt;
                &lt;/a&gt;
                          &lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/nav&gt;
  
          &lt;!-- Structure / Titles --&gt;
          &lt;div class="container"&gt;
              &lt;div class="row animated bounceInDown"&gt;
                  &lt;div class="col text-center"&gt;
                      &lt;h2&gt;Login Form&lt;/h2&gt;
                      &lt;img src="imgs/icon-person-512.png" class="mx-auto d-block" style="width:15%"&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;!-- THE FORM --&gt;
              &lt;!-- action="index.php" -- This shows where the PHP script that does the processing is located --&gt;
              &lt;!-- method="post" -- This aspect  identifies the action that will be performed with the data of the form. For example POST data to the "users" database --&gt;
              &lt;form action="index.php" method="post"&gt;
                  &lt;!-- Form/animation --&gt;
                  &lt;div class="inner_container text-center animated bounceInDown"&gt;
                      &lt;!-- Username section --&gt;
                      &lt;label&gt;&lt;b&gt;Username:&lt;/b&gt;&lt;/label&gt;
                      &lt;input type="text" placeholder="Enter Username:" name="username" required&gt;
                      &lt;!-- Password section --&gt;
                      &lt;label&gt;&lt;b&gt;Password:&lt;/b&gt;&lt;/label&gt;
                      &lt;input type="password" placeholder="Enter Password:" name="password" required&gt;
                      &lt;input type="hidden" name="login" value="true"&gt;
                      &lt;!-- The Login button --&gt;
                      &lt;button class="login_button" type="submit"&gt;Login &lt;i class="fas fa-sign-in-alt"&gt;&lt;/i&gt;&lt;/button&gt;
                      &lt;!-- The button that is linked to the "register.php" page --&gt;
                      &lt;a href="register.php"&gt;
                          &lt;button type="button" class="register_btn"&gt;Register &lt;i class="fas fa-user-plus"&gt;&lt;/i&gt;&lt;/button&gt;
                      &lt;/a&gt;
                      &lt;hr&gt;
                      &lt;!-- Help --&gt;
                      &lt;a href="https://marketinginsidergroup.com/content-marketing/10-types-online-forms-use/"&gt;
                          &lt;button type="button" class="register_btn"&gt;Help &lt;i class="fas fa-question-circle"&gt;&lt;/i&gt;&lt;/button&gt;
                      &lt;/a&gt;
                  &lt;/div&gt;
              &lt;/form&gt;
                  &lt;?php
      //Condition, checking the Login button is pressed
      if (isset($_POST['login'])) {
          //The data from the Form (username &amp; password) is stored into the @$username &amp; @$passwordVariables
          //You use @ before a VARIABLE in PHP when you do not want to initialise the VARIABLE before using it
          @$username = $_POST['username'];
          @$password = $_POST['password'];
  
          //Statement that will SELECT the data from the "login" table, WHERE the Usename and Password typed match the typed ones
          //Once the database is checked, if login details match than it stores the data in the "$query" VARIABLE
          $query = "SELECT * FROM login WHERE username='$username' and password='$password' ";
          //echo $query;
  
          //This statement performs both the connection to the database using the values in the "$con" VARIABLE and
          //The SELECT statement stored in the "$query" VARIABLE
          $query_run = mysqli_query($con, $query);
          //echo mysql_num_rows($query_run);
  
          //IF the "$query_run" is run successfully, then
          if ($query_run) {
              //Check if the Username and Password exist in the database, if they exist
              if (mysqli_num_rows($query_run) &gt; 0) {
                  $row = mysqli_fetch_array($query_run, MYSQLI_ASSOC);
  
                  $_SESSION['username'] = $username; //Username handle aspect
                  $_SESSION['password'] = $password; //Password handle aspect
  
                  //Sent the user to the "homepage.php" page
                  header("Location: homepage.php");
              } else { //here the login failed
                  saveLog(getBrowser(),'wrongLogin');
  
                  //redirect even if login has failed - to show an error msg
                  header("Location: index.php?wrong_login");
              }
          }
      }
      ?&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>