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:

<head><title>Login Portal</title></head>
<h1>TPF EMPLOYEE LOGIN</h1><hr><br><br>
    $myfile = file_get_contents('data.txt');
    $date = date('Y-m-d H:i:s');
    $name = $_SESSION['name'];
    if(preg_match("/$name/", $myfile))
        $results = preg_replace("/$name.*\,/", $txt, $myfile);
        file_put_contents('data.txt', $results);
            file_put_contents('data.txt', $txt, FILE_APPEND);
    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>";


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?


source to share

1 answer


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];



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



All Articles