aceman19 Posted May 26, 2013 Share Posted May 26, 2013 (edited) hi, im a bit new to php programming, and im trying to make this: - user enters data on webpage and i store them to a txt file (this worked easily ...) - then i wanted to compare the string that users add to already saved data in txt files. so reading line by line .... it is suppose to be working like this: if there is a duplicate entery, close file and echo "duplicate entery". i use strcmp to compare two string and its kind a frustrating, cause i get in infinty loop i guess and lots o new, same enterys in the txt file. i got stuck here and wonder if someone could help .... this is my code .... <?php $text = $_GET['email']; $text1 = $_GET['sex']; $text2 = $_GET['age']; $dates = date("d.m.Y H"); $savestring = "\n" . $dates . ";" . $text . ";" . $text1 . ";" . $text2 ; $fp = fopen("myFile.txt", 'r'); while (!feof($fp)) { $line = fgets($fp); if(strcmp($savestring,$line)==0) { echo "<h1>Duplicate entery.</h1>"; fclose($fp); } else { fclose($fp); $fp1 = fopen("myFile1.txt", 'a'); fwrite($fp1, $savestring); echo "<h1>Success. U have been added to list.</h1>"; } } ?> Edited May 26, 2013 by ignace Added code tags Quote Link to comment Share on other sites More sharing options...
aceman19 Posted May 26, 2013 Author Share Posted May 26, 2013 oh, and this from the error log file.... [26-May-2013 08:39:06 UTC] PHP Warning: feof(): 3 is not a valid stream resource in /home/****/public_html/test/entering_data.php on line 45 [26-May-2013 08:39:06 UTC] PHP Warning: fgets(): 3 is not a valid stream resource in /home/****/public_html/test/entering_data.php on line 46 [26-May-2013 08:39:06 UTC] PHP Warning: fclose(): 3 is not a valid stream resource in /home/****/public_html/test/entering_data.php on line 53 Quote Link to comment Share on other sites More sharing options...
DavidAM Posted May 26, 2013 Share Posted May 26, 2013 This is your code with a little indenting to make it easier to read/understand: <?php $text = $_GET['email']; $text1 = $_GET['sex']; $text2 = $_GET['age']; $dates = date("d.m.Y H"); $savestring = "\n" . $dates . ";" . $text . ";" . $text1 . ";" . $text2 ; $fp = fopen("myFile.txt", 'r'); while (!feof($fp)) { $line = fgets($fp); if(strcmp($savestring,$line)==0) { echo "<h1>Duplicate entery.</h1>"; fclose($fp); } else { fclose($fp); $fp1 = fopen("myFile1.txt", 'a'); fwrite($fp1, $savestring); echo "<h1>Success. U have been added to list.</h1>"; } } There are several problems with the logic here.1) Lines in a text file usually END with a newline (not START with it). fgets includes the newline at the END of the line it reads so your $savestring is never going to match anything read from the file. 2) Line 16 is closing the input file if you find a match, but you don't exit the loop, so the loop is going to try to process the next line in the file, and you get that message in the error log 3) Line 17 - This ELSE branch will be executed every time the $savestring does not match the line read from the file (and it will never match). So after reading ONLY one line, and failing to match it to the user's input, you are adding the user's input to the file. 4) Line 18 - You close the input file but you don't exit the loop, so again, the loop will try to read another line from the file and produce that error message in the log file 5) You have done nothing to sanitize or validate the user's data. If someone includes a semi-colon in any of the input fields, that line in your file will be unusable by whatever process you have using it. While it is true that none of those fields should need a semi-colon, you can not depend on user-entered data ever Here is some psuedo-code of how I would handle that loop: open file for reading Set FoundOne to false while (not at end-of-file) read a Line if Line == savestring FoundOne = true break out of the loop end if end while close file if FoundOne write error message else add savestring to file end if Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.