newbtophp Posted October 17, 2009 Share Posted October 17, 2009 How would i log submits on a form, by everyone? I want to then echo the number of submissions. I've tried this: <?php if (isset($_POST['submit'])) { $myFile = "testFile.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, $submit); fclose($fh); } ?> But unsure of what to do next... :-\ Link to comment https://forums.phpfreaks.com/topic/178056-solved-log-_post/ Share on other sites More sharing options...
teynon Posted October 17, 2009 Share Posted October 17, 2009 If your going to do a counter file, I would do $myFile="testFile.txt"; $count=file_get_contents($myFile); if ($fh=fopen($myFile, 'w')) { if (fwrite($fh, $count++)) { echo $count; } fclose($fh); } Link to comment https://forums.phpfreaks.com/topic/178056-solved-log-_post/#findComment-938831 Share on other sites More sharing options...
newbtophp Posted October 17, 2009 Author Share Posted October 17, 2009 teynon i tried your code and its not working: <form method="post" enctype="multipart/form-data" action="<?=$_SERVER["PHP_SELF"]?>"> <input name="file" type="file" /> <input type="submit" name="submit" value="submit" /> </form> <?php if (isset($_FILES['file'])) { $file = file_get_contents($_FILES['file']['tmp_name']); $submit = $_POST['submit']; if($file == "") echo "Error!"; else { $myFile="count.txt"; $count=file_get_contents($myFile); if ($fh=fopen($myFile, 'w')) { if (fwrite($fh, $count++)) { echo $count; } fclose($fh); } } } ?> Everytime i click submit its meant to increase the count number by 1, buts currentlys its not doing anything :-\ Link to comment https://forums.phpfreaks.com/topic/178056-solved-log-_post/#findComment-938841 Share on other sites More sharing options...
teynon Posted October 17, 2009 Share Posted October 17, 2009 $myFile="testFile.txt"; $count=file_get_contents($myFile); if ($fh=fopen($myFile, 'w')) { if (fwrite($fh, $count++)) { echo $count; } else { die("Failed to write to {$myFile}"); } fclose($fh); } else { die("Failed to open {$myFile}"); } Link to comment https://forums.phpfreaks.com/topic/178056-solved-log-_post/#findComment-938844 Share on other sites More sharing options...
newbtophp Posted October 17, 2009 Author Share Posted October 17, 2009 @teynon I have the file correctly placed and chmodded 777, still no counts are logged to testFile.txt Link to comment https://forums.phpfreaks.com/topic/178056-solved-log-_post/#findComment-938848 Share on other sites More sharing options...
ialsoagree Posted October 17, 2009 Share Posted October 17, 2009 I'm not sure if this is the problem but try changing $count=file_get_contents($myFile); to $count=(int)file_get_contents($myFile); Link to comment https://forums.phpfreaks.com/topic/178056-solved-log-_post/#findComment-938850 Share on other sites More sharing options...
teynon Posted October 17, 2009 Share Posted October 17, 2009 When you run the script with the code I just added, does it die and give you one of those messages? Link to comment https://forums.phpfreaks.com/topic/178056-solved-log-_post/#findComment-938851 Share on other sites More sharing options...
MySQL_Narb Posted October 17, 2009 Share Posted October 17, 2009 <?php $count_my_page = ("hitcounter.txt"); $hits = file($count_my_page); $hits[0] ++; $fp = fopen($count_my_page , "w"); fputs($fp , "$hits[0]"); fclose($fp); echo $hits[0]; ?> It works, it's what I use. Link to comment https://forums.phpfreaks.com/topic/178056-solved-log-_post/#findComment-938856 Share on other sites More sharing options...
teynon Posted October 17, 2009 Share Posted October 17, 2009 Oops, change $count++ to ++$count. Putting ++ after the variable will send that variable and then increase it. Putting it before will increase it then put it. Link to comment https://forums.phpfreaks.com/topic/178056-solved-log-_post/#findComment-938859 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.