简体   繁体   English

在txt文件中搜索和覆盖时间戳

[英]Searching and Overwritting timestamp in txt file

I have a problem in php. 我在php中有问题。 I was asked to do this for my college purpose. 我被要求这样做是出于我的大学目的。 I have a basic textbox in my first page and the user is to enter a login id in it and press the login button. 我的第一页中有一个基本的文本框,用户要在其中输入登录ID,然后按登录按钮。 Once the login button is pressed the login id and timestamp is stored in a txt file separated by comma. 按下登录按钮后,登录ID和时间戳将存储在以逗号分隔的txt文件中。 So if it is the 2nd time user A logs in then the previous timestamp must be overwritten with the new one. 因此,如果这是用户A第二次登录,则先前的时间戳必须用新的时间戳覆盖。

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 = fopen("data.txt", "a") or die("Unable to open file!");
    $_SESSION['name']=$_POST['id'];
    date_default_timezone_set('Asia/Calcutta');
    $date = date('Y-m-d H:i:s');
    $txt=$_SESSION['name'].",".$date.",\n";
    fputs($myfile, $txt);
    fclose($myfile);
    }
    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 how my txt file looks like : 这是我的txt文件的样子:

yesh,2014-10-05 10:00:42,
thilak,2014-10-05 10:00:56,
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>";
}

Make sure you test this carefully before using it on your real file, to ensure it doesn't harm your data. 在将其用于实际文件之前,请确保已仔细测试,以确保它不会损害您的数据。 Because I don't know enough about the file itself to be sure it doesn't damage the file. 因为我对文件本身不太了解,所以不能确定它不会损坏文件。

Something like this (though probably not optimal) should work. 这样的事情(虽然可能不是最佳的)应该起作用。 However, in a real environment you would probably want to use a database or at least lock the file so it's not updated by another session between your read and write. 但是,在实际环境中,您可能要使用数据库或至少锁定文件,以免读写之间的另一个会话更新该文件。 This is untested but should set you in the right direction. 这未经测试,但应该为您设定正确的方向。

$lines = file($file, FILE_IGNORE_NEW_LINES);
$name = preg_quote($_SESSION['name']);
$lines = preg_replace('/^' . $name . ',([^,]+),/$', $date, $lines);
file_put_contents($file, join("\n", $lines));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM