Tonic-_- Posted July 19, 2009 Share Posted July 19, 2009 Don't really mind the subject, i didn't know what to label it as but I am fiddling with php and for some odd reason I have tried this about 6 different ways and the coding below is the most accurate way I could think of checking a file & writing the new username to the file.. <?php $open = fopen("users.txt", "r") or die("Error, recheck script."); $usernames = $_GET['user']; $users = explode("\n", $open); foreach($users as $user) { if (!$user == $usernames) { $useropen = fopen("users.txt", "a") or die("Error, recheck script."); $newuser = $usernames . "\n"; fwrite($useropen, $newuser); fclose($useropen); echo "New user added"; } } fclose($open); ?> Now the problem is no matter what way I try it will not write the URL requested ?user=??? to the file period. I can even take it outside the foreach part and it won't write the name. And as you see I have it set to write if $usernames is not equal to $user (the new lines of users.txt) and it still doesn't write flat out period. I'm dead tired and don't see wtf is up with it. I got it to write a name ONCE and then deleted the .txt and re-uploaded it and re-chmodded it to 0777 and still nada. Idk wtf is up.. Any clues? Quote Link to comment https://forums.phpfreaks.com/topic/166494-solved-problem-with-checking-array-writing/ Share on other sites More sharing options...
Daniel0 Posted July 19, 2009 Share Posted July 19, 2009 Like this? $username = $_GET['user']; $users = file('users.txt'); if (!in_array($username, $users)) { $fp = fopen('users.txt', 'a'); fwrite($fp, $username . PHP_EOL); fclose($fp); } You might want to do some validation on the username though. Otherwise you could insert literally anything into the file. Quote Link to comment https://forums.phpfreaks.com/topic/166494-solved-problem-with-checking-array-writing/#findComment-877968 Share on other sites More sharing options...
Tonic-_- Posted July 19, 2009 Author Share Posted July 19, 2009 Ehh sort of, trying to get it to check users.txt and see if any names match the name in $_GET['user'], if it does match then it won't write the name, but if it doesn't match it will write the new name from user to users.txt... That alone adds the user but it doesn't add that check part in that I been seeking originally. Quote Link to comment https://forums.phpfreaks.com/topic/166494-solved-problem-with-checking-array-writing/#findComment-877972 Share on other sites More sharing options...
wildteen88 Posted July 19, 2009 Share Posted July 19, 2009 Thats what Dans code does. Quote Link to comment https://forums.phpfreaks.com/topic/166494-solved-problem-with-checking-array-writing/#findComment-877974 Share on other sites More sharing options...
Tonic-_- Posted July 19, 2009 Author Share Posted July 19, 2009 Arg.. I used his coding directly and it can still add the same name multiple times to the .txt Example... refreshed twice wrote Tonic Tonic i'm like dead tired and can't process much atm but idk this is just so damn aggravating. Quote Link to comment https://forums.phpfreaks.com/topic/166494-solved-problem-with-checking-array-writing/#findComment-877975 Share on other sites More sharing options...
wildteen88 Posted July 19, 2009 Share Posted July 19, 2009 Change this line to $users = file('users.txt'); to $users = array_map('trim', file('users.txt')); Quote Link to comment https://forums.phpfreaks.com/topic/166494-solved-problem-with-checking-array-writing/#findComment-877978 Share on other sites More sharing options...
Daniel0 Posted July 19, 2009 Share Posted July 19, 2009 Sorry, change it to $users = array_map('trim', file('users.txt')); Edit: Damn... beat me to it Quote Link to comment https://forums.phpfreaks.com/topic/166494-solved-problem-with-checking-array-writing/#findComment-877979 Share on other sites More sharing options...
Tonic-_- Posted July 19, 2009 Author Share Posted July 19, 2009 TYVM. Originally I started off using $file = file( blah) but I kept getting issues every flip way I tried it then I said to hell with it lets try it simple & basic and then it completely failed. But TYVM I <3 this site. So useful Quote Link to comment https://forums.phpfreaks.com/topic/166494-solved-problem-with-checking-array-writing/#findComment-877983 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.