Kingy Posted December 22, 2007 Share Posted December 22, 2007 I have a small php script that adds users to a text file. This works perfectly fine but what i want to do is.. read the users out of the text file and make it so any one of the users can do things on the website.. eg: if ($user == *a user out of the .txt file*) { echo "You can use all commands"; } else { echo "Sorry you arn't authorized to use the commands"; } how would i go about doing something like this.. NB: in the text file it works looks like this USER1 USER2 USER3 etc etc Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/ Share on other sites More sharing options...
Daniel0 Posted December 22, 2007 Share Posted December 22, 2007 <?php $users = file('users.txt'); if(in_array($user, $users)) { echo 'User exists'; } else { echo 'User does not exist'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421108 Share on other sites More sharing options...
Kingy Posted December 22, 2007 Author Share Posted December 22, 2007 oh thank you heaps... also how would i make it list all the users like that? would i use a while() loop or...? Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421109 Share on other sites More sharing options...
Daniel0 Posted December 22, 2007 Share Posted December 22, 2007 Just use a regular foreach loop. file() stores all the lines from a file in an array. <?php $users = file('users.txt'); foreach($users as $user) { echo $user . PHP_EOL; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421111 Share on other sites More sharing options...
BenInBlack Posted December 22, 2007 Share Posted December 22, 2007 i don't know what the text file, but if it is more than one piece of information per line then the in_array method wont work. you can use the stripos to do a contains if (stripos($filecontents,$user) !== false) { echo "You can use all commands"; } else { echo "Sorry you arn't authorized to use the commands"; } Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421113 Share on other sites More sharing options...
Kingy Posted December 22, 2007 Author Share Posted December 22, 2007 Bot.php if($cmd == ":`test") { $owners = file('owners.txt'); if(in_array(Kingy, $owners)) { fputs($conn,"PRIVMSG #Lobby : Hi Kingy \r\n"); } else { fputs($conn,"PRIVMSG #Lobby : Don't no you \r\n"); } } owners.txt Kingy that is the code i'm using. Does that look right or is anything missing? Everytime i use the command `test i get "Don't no you". Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421152 Share on other sites More sharing options...
Daniel0 Posted December 22, 2007 Share Posted December 22, 2007 Change if(in_array(Kingy, $owners)) { to if(in_array('Kingy', $owners)) { Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421154 Share on other sites More sharing options...
Kingy Posted December 22, 2007 Author Share Posted December 22, 2007 Thank you that worked! I have a weird problem now though. If i manually put "Kingy" into the text file it responds properly and replies "Hi Kingy" whereas if i type `addowner Kingy as the command it inserts it into the text file and it looks just the same as it does when i manually put it in but i get "Don't know you" as the reply. $file = fopen("owners.txt","a+"); $owner = $ex[4]; fwrite($file, "$owner"); fclose($file); is there any problem in there with that? Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421161 Share on other sites More sharing options...
Daniel0 Posted December 22, 2007 Share Posted December 22, 2007 You might want to add a newline before the name, i.e. "\n$owner". Try to check if it has the correct format after you ran the command. Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421164 Share on other sites More sharing options...
Kingy Posted December 22, 2007 Author Share Posted December 22, 2007 well currently when i write to the text file it is already putting the names on a new line. Name1 Name2 Name3 and as soon as i implement the \n it starts going Name1 Name2 Name3 any idea why this is? Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421167 Share on other sites More sharing options...
Daniel0 Posted December 22, 2007 Share Posted December 22, 2007 Well, then don't use the new line. Do you reload the the owners list after running your addowner command? Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421170 Share on other sites More sharing options...
Kingy Posted December 22, 2007 Author Share Posted December 22, 2007 do i need to? if the bot is running and i manually edit the owners.txt and then running the `test command it works, and when i `addowner i manually check the owners.txt and the name appears in there, but the `test command doesnt respond? Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421172 Share on other sites More sharing options...
Daniel0 Posted December 22, 2007 Share Posted December 22, 2007 Do you load the owners file on each `test command? You'll have to or you'll have to load it at the start and then reload it when if you change it while the script is running. Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421174 Share on other sites More sharing options...
Kingy Posted December 22, 2007 Author Share Posted December 22, 2007 if($cmd == ":`addowner") { $file = fopen("owners.txt","a"); $owner = $ex[4]; $owner = str_replace(' ', "","\n$owner"); fwrite($file, "$owner"); fclose($file); } if($cmd == ":`test") { $owners = file('owners.txt'); if(in_array("Kingy", $owners)) { fputs($conn,"PRIVMSG #Lobby Hi Kingy \r\n"); } else { fputs($conn,"PRIVMSG #Lobby Don't no you \r\n"); } } that there is the code for this hole thing so far... do you see anything specific as to why it won't work when i `addowner rather than having to manually do it Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421175 Share on other sites More sharing options...
Kingy Posted December 23, 2007 Author Share Posted December 23, 2007 I'm still having the same problem. If i manually write 'Kingy' into the txt file the bot will respond with 'Hi Kingy' when i type the `test command. But as soon as i type '`addowner Kingy' and then type `test it responds with 'Don't no you'. I have a feeling that when it writes a name to the txt file using `addowner that it might be including some sort of character or something and so then when it tries to read it to see if the user exists it will read something different. Does anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421507 Share on other sites More sharing options...
Daniel0 Posted December 23, 2007 Share Posted December 23, 2007 Try to use trim() when adding the user to the file. Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421609 Share on other sites More sharing options...
Kingy Posted December 23, 2007 Author Share Posted December 23, 2007 ahh, brilliant. thats works like a charm. Its nearly there!!! lol. Now the major problem i have now is, the bot works well with one user but as soon as i add a new user... User1 User2 it will only respond to the user at the bottom.. so then if i got like User1 User2 User1 it will respond to user1 again but not user2.. help !! Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421660 Share on other sites More sharing options...
Daniel0 Posted December 23, 2007 Share Posted December 23, 2007 Isn't the problem just that you have duplicate entries? Check that the owner is not already in the list before adding him/her and optionally display a message to the user stating that the owner is already in the owner list. Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421664 Share on other sites More sharing options...
Kingy Posted December 23, 2007 Author Share Posted December 23, 2007 its not just that... `addowner User1 so now owners.txt reads: User1 then i will type the command `test and it will respond Hi User1. `addowner User2 owners.txt... User1 User2 (my nick is still User1) `test and it responds "Don't know you" (my nick is now User2) `test and it responds "Hi User2". but if i add a duplicate entry of User1 (`addowner User1). owners.txt is now: User1 User2 User1 (my nick is still User1) `test and it responds "Hi User1" (my nick is now User2) `test and it responds "Dont know you". I don't think it has anything to do with the fact that i have duplicate entries, it will for some reason only read the name on the bottom line? Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421669 Share on other sites More sharing options...
Daniel0 Posted December 23, 2007 Share Posted December 23, 2007 When loading the file like $owners = file('owners.txt'); try to add print_r($owners); below it and post the output here. Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421681 Share on other sites More sharing options...
Kingy Posted December 23, 2007 Author Share Posted December 23, 2007 Array ( [0] => Kingy [1] => K1 ) thats the response i get Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421981 Share on other sites More sharing options...
Daniel0 Posted December 23, 2007 Share Posted December 23, 2007 Array ( [0] => Kingy [1] => K1 ) thats the response i get Then it should work with in_array() Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-422034 Share on other sites More sharing options...
Kingy Posted December 24, 2007 Author Share Posted December 24, 2007 i think it may have something to do with the \r\n and stuff now.. i'm not sure but now it doesnt read it at all :S lol this is crazy Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-422056 Share on other sites More sharing options...
Kingy Posted December 24, 2007 Author Share Posted December 24, 2007 ok i have it working again, but it's still only working for the last user that is added! i dont get it though, because surely if prints like Array ( => Kingy [1] ) etc then surely if the code is if(in_array()) then it would work for every1 :S Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-422064 Share on other sites More sharing options...
Kingy Posted December 24, 2007 Author Share Posted December 24, 2007 This is still really crazy, and it some what frustrating me and i don't understand why, I won't need some sort of loop or anything so that it will read all users in the txt file. I am still quite new to these flat file functions so its still all a bit confusing to me Quote Link to comment https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-422340 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.