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 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加