I’m a total novice to php but I’ve been trying to put together a form that writes data to a text file, probably sounds simple but also need it to do the following: 1. After clicking on submit I want the data written to a text file 2. with a time stamp 3. and the form must reset all values except the user name value. Ready for another submission Each submission must append the text file. Can anyone help? Many Thanks in Advance.
Here is a little something I wrote to keep up with your most recent bot visits. I do not append the stuff to a file, I write over it and only save the most recent visit. You can change that by making the "w+" an "a+" in the code below. Hope this is what you are looking for. if ($_SERVER['HTTP_USER_AGENT'] == 'msnbot/1.0 (+http://search.msn.com/msnbot.htm)') {$user_agent = 'MSNbot_1.0,'.date("Y-m-d H:i:s").'\n' ; $file_name = 'MSNbot.txt' ;} if ($_SERVER['HTTP_USER_AGENT'] == 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)') {$user_agent = 'Googlebot,'.date("Y-m-d H:i:s").'\n'; $file_name = 'Googlebot.txt' ;} if ($_SERVER['HTTP_USER_AGENT'] == 'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)') {$user_agent = 'Yahoo_Slurp,'.date("Y-m-d H:i:s").'\n'; $file_name = 'Yslurp.txt' ;} $myFile = "/path/to/save/" . $file_name; $fh = fopen($myFile, 'w+') or die("can't open file"); fwrite($fh, $user_agent); fclose($fh); PHP: Once that information has been saved to the file, it is in csv format so you can $pieces = explode(",",$file); echo $pieces[0] ; //get the bot name echo $pieces[1] ; //get the date of last visit PHP:
Is it the same text file or a new file for each submission??? $path_to_my_file="./files/my_file_name.txt"; $my_file=fopen($path_to_my_file,'r+'); // or 'a+' if you want to add to the end of the file fwrite($my_file,"The text will be added to the file"); fclose($my_file); PHP: In the value property of the fields you don't won't to reset: <input name="keepAlive" value="<?php echo $_POST['KeepAlive'];?>" /> PHP: I'm a noob too so don't expect it to work lol
I wrote a simple page to track all users ip and browser information and this is how I write the values to the text file. if($FP = fopen($TextFile, "a")) { $log = ''.$FormattedDate.' '.$FormattedTime.' '.$line1."\n"; fwrite($FP, $log); fclose($FP); $ferror = 1; } else if($ferror == 0) { $ErrorFile = "errorlogs/file_error.txt"; if($FP = fopen($ErrorFile, "a")) { $log = ''.$FormattedDate.' '.$FormattedTime.' '.$line1." Could not Open: ".$TextFile."\n"; fwrite($FP, $log); fclose($FP); } } I usually don't need to save any variables after the file has been written too but like nabil has posted you can use the <input name="keepAlive" value="<?php echo $_POST['KeepAlive'];?>" /> to keep the data alive.