Show previous recorded timestamp from txt file

I was asked to do this with the help of my college staff, kindly help me with that! I have a php file with a textbox and a login id must be entered into it and the login button must be clicked. After clicking this button, login ID and timestamp are saved in txt file. The next time the same login ID is used, the timestamp must be overwritten. I have completed this part successfully. Now I want to display the timestamp before overwriting it. This is something similar to the last whatsapp seen. How can I display it?

This is my code:

<html>
<head><title>Login Portal</title></head>
<body><center>
<h1>TPF EMPLOYEE LOGIN</h1><hr><br><br>
<?php
session_start();
if(isset($_POST['submit']))
    {
    $myfile = file_get_contents('data.txt');
    $_SESSION['name']=$_POST['id'];
    date_default_timezone_set('Asia/Calcutta');
    $date = date('Y-m-d H:i:s');
    $txt=$_SESSION['name'].",".$date.",\n";
    $name = $_SESSION['name'];
    if(preg_match("/$name/", $myfile))
    {
        $results = preg_replace("/$name.*\,/", $txt, $myfile);
        file_put_contents('data.txt', $results);
    }
    else
        {
            file_put_contents('data.txt', $txt, FILE_APPEND);
        }
    }
    else
    {
    echo "<form name='login' method='post'>";
    echo "Enter your login id : <input type='text' name='id' id='id' /><br><br>";
    echo "<input type='submit' name='submit' value='Login' />";
    echo "</form>";
    }
?>
</center>
</body>
</html>

      

This is the content of my txt file:

a,2014-10-05 19:00:40,

b,2014-10-05 19:00:31,

      

Using a comma after the name as an identifier, how can I display the previous timestamp before overwriting it?

+3


source to share


1 answer


Edit:

if(preg_match("/$name/", $myfile))
{

      

to include $ match and change the regexp and then work with the $ match array:



if(preg_match("/$name\,(.*),/", $myfile, $matches))
{       
   echo 'Previous Login: ' . $matches[1];

      

Example: http://ideone.com/1diNNd

Tips: use db, make sure $ name is unique ...

0


source







All Articles