download92000 2014-10-05 13:56
浏览 73
已采纳

显示以前从txt文件登录的时间戳

I was asked to do this by my college staffs so kindly help me out with this! I have a php file with a text box and login id is supposed to be entered in it and login button is to be pressed. Once this button is pressed, the login id and timestamp is stored in a txt file. Next time the same login id is used then the timestamp is to be overwritten. I have done this part successfully. Now, i wanna display the timestamp before overwritting it. This is something similar to last seen of whatsapp. 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.",
";
    $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 contents of my txt file:

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

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

Using the comma after the name as an identifier how do i display the previous timestamp before overwritting it?

  • 写回答

1条回答 默认 最新

  • dongtiao0279 2014-10-05 14:06
    关注

    Change:

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

    to include $matches and alter the regexp, then work with $matches array:

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

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

    Hints: use a db, ensure $name is unique...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)