Tuesday, 15 February 2011

php - Searching and Overwritting timestamp in txt file -



php - Searching and Overwritting timestamp in txt file -

i have problem in php. asked college purpose. have basic textbox in first page , user come in login id in , press login button. 1 time login button pressed login id , timestamp stored in txt file separated comma. if 2nd time user logs in previous timestamp must overwritten new one.

this 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 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 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 how txt file looks :

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 login id : <input type='text' name='id' id='id' /><br><br>"; echo "<input type='submit' name='submit' value='login' />"; echo "</form>"; }

make sure test before using on real file, ensure doesn't harm data. because don't know plenty file sure doesn't harm file.

php html

No comments:

Post a Comment