ddemaree Posted April 24, 2008 Share Posted April 24, 2008 I'm new to the whole web page design thing. i'm trying to make a very simple script thats writes only unique entries to a txt file but i dont know how th disallow duplicates. i need to know how to do that. i would like the script as small and simple as possible and security is no issue as the data is not vital. also i need a script that redirects to a random page chosen from a line on a text file in a frame with an menu to change the delay time without repeating and pages. the second request i haven't started on yet so i might be able to figure that out. in its entirety i dont want the script using anything but html, php, and js. no databases. heres what i got so far. --------------------------------------------------- in folder "usrs" "index.htm" "usr.php" "usr.txt" ===================================== usrs/index.htm ----------------------------------------------------------------------------- <html> <body> <SCRIPT language=javascript> <!-- function onlyNum(evt) { var charCode = (evt.which) ? evt.which : event.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) return false; return true; } //--> </SCRIPT> <form action="usr.php" method="post"> User: <input type="text" name="usr" onkeypress="return onlyNum(event)" /> <input type="submit" /> </form> </body> </html> ================================================================= usrs/usr.php ------------------------------------------------------------------------------------------- <?PHP header("refresh: 3; url=http://localhost/usrs"); $usrs = "usr.txt"; $input = $_POST['usr']; $usrp = fopen ($usrs, "a+"); if ($usrp) { fwrite ($usrp, "$input\r\n"); fclose ($usrp); echo ("<center>Database updated<br>Thank you for the submission.</center>"); } else { echo ("An error occurred and database was not updated"); } ?> =================================================================== usrs/usr.txt (examples only) ---------------------------------------------------------------------------------------------- 012345 123456 234567 =================================================================== The script works but i do not know how to disallow duplicate entries Quote Link to comment https://forums.phpfreaks.com/topic/102791-very-new-very-simple/ Share on other sites More sharing options...
SharkBait Posted April 24, 2008 Share Posted April 24, 2008 You could read the values from the text file into an array and then check the entry to see if it already exists in the file. If it doesn't then you could append the new entry to the end of the file. Quote Link to comment https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526511 Share on other sites More sharing options...
Daniel0 Posted April 24, 2008 Share Posted April 24, 2008 Something like: <?php header("refresh: 3; url=http://localhost/usrs"); $usrs = "usr.txt"; $input = $_POST['usr']; $lines = file($usrs); $usrp = fopen ($usrs, "a+"); if (in_array($input, $lines)) { echo 'duplicate entry'; } else if ($usrp) { fwrite ($usrp, "$input\r\n"); fclose ($usrp); echo ("<center>Database updated<br>Thank you for the submission.</center>"); } else { echo ("An error occurred and database was not updated"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526520 Share on other sites More sharing options...
ddemaree Posted April 24, 2008 Author Share Posted April 24, 2008 so does that mean i have to assign my output to a variable in an array? ive only been studying php for about a week andi really dont know html or js too well. i want to learn it all better but i'd like to get this script done first. Quote Link to comment https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526545 Share on other sites More sharing options...
ddemaree Posted April 24, 2008 Author Share Posted April 24, 2008 if i have to make the output a different filetype i can. in fact would it help with the second half if the project to do it that way? Quote Link to comment https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526551 Share on other sites More sharing options...
Daniel0 Posted April 24, 2008 Share Posted April 24, 2008 I don't understand either of your two questions you just posted. Quote Link to comment https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526556 Share on other sites More sharing options...
ddemaree Posted April 24, 2008 Author Share Posted April 24, 2008 lol, that dont surprise me, i dont know what i'm talking about. instead of writing the usr to a txt file would it help to write it to a js file as a js variable? and if i did that could i put a js snippet in there that checks to see if the variable exists and if it doesn't add it to the js file? does this make sense? Quote Link to comment https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526560 Share on other sites More sharing options...
Daniel0 Posted April 24, 2008 Share Posted April 24, 2008 That wouldn't be a good idea. You could however utilize Javascript in form of AJAX to do some validation on the client side before the form is submitted. You should however still check it in your PHP script though. Do a Google search for AJAX. There are plenty of resources out there. Quote Link to comment https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526562 Share on other sites More sharing options...
ddemaree Posted April 24, 2008 Author Share Posted April 24, 2008 ok, i'll do that. what should do to make a javascript random redirect script that reads from my txt file and doesn't repeat url's. would i have to use cookies for this? Quote Link to comment https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526565 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.