title: sort informations from a file .txt using php
body: <p>I use php in order to save all informations concerning my user (where do they navigated and at which time).</p>
  
  <p>So I use the following code for saving informations:</p>
  
  <pre><code>&lt;?php
  function iif($condition, $true, $false ) {
      return ($condition ? $true : $false);
  }
  
  $ipaddress = $_SESSION['login'];
  $page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}";
  $page .= iif(!empty($_SERVER['QUERY_STRING']), "?{$_SERVER['QUERY_STRING']}", "");
  $referrer = $monUrl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 
  $datetime = mktime();
  $useragent = $_SERVER['HTTP_USER_AGENT'];
  $remotehost = @getHostByAddr($ipaddress);
  
  // Create log line
  $logline = $ipaddress . '|' . $referrer . '|' . $datetime . '|' . $useragent . '|' . $remotehost . '|' . $page . "\n";
  // Write to log file:
  $logfile = 'logfile.txt';
  $path='log/'.$_SESSION['login'].'/'.$logfile;
  if(!file_exists($path)) {
  mkdir('log');
  mkdir('log/'.$_SESSION['login']);
  $f = fopen('log/'.$_SESSION['login'].'/'.$logfile, "x+");
  // fermeture
  fclose($f);
  
  }
  // Open the log file in "Append" mode
  if (!$handle = fopen($path, 'a+')) {
  die("Failed to open log file");
  }
  // Write $logline to our logfile.
  if (fwrite($handle, $logline) === FALSE) {
  die("Failed to write to log file");
  }
  fclose($handle);
  
  ?&gt;
  </code></pre>
  
  <p>And I use the following code below to display it:</p>
  
  <pre><code>                                   &lt;?php 
  
  
  $path= 'log/'.$data['login'].'/logfile.txt';
  $logfile = $path;
  if (file_exists($logfile)) {
  $handle = fopen($logfile, "r");
  $log = fread($handle, filesize($logfile));
  fclose($handle);
  } else {
  die ("Le fichier de Log n'existe pas!");
  }
  // Seperate each logline
  $log = explode("\n", trim($log));
  //After that it may be useful to get each part of each logline in a separate variable. This can be done //by looping through each logline, and using explode again:
  // Seperate each part in each logline
  for ($i = 0; $i &lt; count($log); $i++) {
  $log[$i] = trim($log[$i]);
  $log[$i] = explode('|', $log[$i]);
  }
  
  // Show a table of the logfile
  echo '&lt;table id="box-table-a"&gt;';
  echo '&lt;th scope="col"&gt;Agent&lt;/th&gt;';
  echo '&lt;th scope="col"&gt;Referrer&lt;/th&gt;';
  echo '&lt;th scope="col"&gt;Date&lt;/th&gt;';
  echo '&lt;th scope="col"&gt;Useragent&lt;/th&gt;';
  echo '&lt;th scope="col"&gt;Remote Host&lt;/th&gt;';
  foreach ($log as $logline) {
  echo '&lt;tr&gt;';
  echo '&lt;td&gt;&lt;a href="index.php?p=voir_fiche_employee&amp;id='.$_GET['id'].'"/&gt;' . $logline['0'] . '&lt;/a&gt;&lt;/td&gt;';
  echo '&lt;td&gt;&lt;a href="'.urldecode($logline['1']).'"/&gt;'. urldecode($logline['1']) . '&lt;/a&gt;&lt;/td&gt;';
  echo '&lt;td&gt;' . date('d-m-Y h:i:s', $logline['2']) . '&lt;/td&gt;';
  echo '&lt;td&gt;' . $logline['3'] . '&lt;/td&gt;';
  echo '&lt;td&gt;' . $logline['4'] . '&lt;/td&gt;';
  echo '&lt;/tr&gt;';
  }
  echo '&lt;/table&gt;';
  ?&gt;
  </code></pre>
  
  <p>The trouble I met is that I really do not know how to sort informations from this file.
  In fact on the top of the page I have a form in order to choose between the dates. I wsould like to display all informations from this logfile between those dates but I really do not know how to do.</p>
  
  <p>At the beginning I was planning to save all in the databse, but somebody told me that it would have been too much for the databse, so I decided to save all in a txt on the server.</p>
  
  <p>Does anyone know if it is possible to sort informations from txt file?</p>
  
  <p>By default I would like to display informations of the logfile for the date of the day, and If I want I can choose an other dates or an interval of dates.</p>