fritz.fx Posted July 18, 2008 Share Posted July 18, 2008 Hey guys I've written a standard php-mysql driven 'members' script to allow "registered" members access to certain parts of my site, which works like a charm, only thing I'd like to restrict registration to usernames already approved' So far I've managed to do it by putting this in the script > //if the name does not exist it gives an error if ($check2 != 0) { die('Sorry, the username '.$_POST['username'].' is already in use.'); } //if username equals a clan member if($_POST['username'] == 'A Clan members name I have to manually insert into the script here') { } else { die ('The username you have submitted does not have the permissions to access this resource....} //carry on if that name == A Clan members name Only problem with this way I have to do it 50 times for each clan member, which is pretty messy as you could imagine. So to get around the messy part I have the names of those names allowed in a text file (one per line) and am trying to get the script to check the name submitted in the registration form against a name in the text file. So far I have this > //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the username '.$_POST['username'].' is already in use.'); } //if username equals a clan member $lines = file('names.txt'); foreach ($lines as $line_num) => $line) if($_POST['username'] == $line) { } else { die ('The username you have submitted does not have the permissions to access this resource....} //carry on if that name == A Clan members name My problem is, is that it's refusing access to any name, whether on the list or not. Can anybody see where I'm going wrong?? Quote Link to comment https://forums.phpfreaks.com/topic/115435-solved-foreach-in-a-text-file/ Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 Okay.. Try it like this. Your file is like this: member1||member2||member3||member4||member5||member6||member7 etc etc... Then you can do it like so: $names = explode("||", file_get_contents("names.txt")); //put each name into an array if(in_array($_POST['username'],$names)){ die("Name is a clan member name!"); }else{ //its all good! } Or you can just build the array yourself... $names = array("member1","member2","member3","member4","member5"); if(in_array($_POST['username'],$names)){ die("Name is a clan member name!"); }else{ //its all good! } Quote Link to comment https://forums.phpfreaks.com/topic/115435-solved-foreach-in-a-text-file/#findComment-593437 Share on other sites More sharing options...
fritz.fx Posted July 18, 2008 Author Share Posted July 18, 2008 Went for your first example.. All I can say is,, Man, that is sweet, works like a charm, thankyou very much, I've been at this for hours, and would have logged a few more if it weren't for you.. Cheers Fritz Quote Link to comment https://forums.phpfreaks.com/topic/115435-solved-foreach-in-a-text-file/#findComment-593508 Share on other sites More sharing options...
JasonLewis Posted July 18, 2008 Share Posted July 18, 2008 First one is easier. Glad to help. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/115435-solved-foreach-in-a-text-file/#findComment-593530 Share on other sites More sharing options...
fritz.fx Posted July 18, 2008 Author Share Posted July 18, 2008 Thanks again might give your second example tomorrow (when I'm a little more sober ) But just out of curiousness could I arrange my 'names.txt' file so that every name is on a new line and change the script so that it looks like this> names = explode("\rn", file_get_contents("names.txt")); //put each name into an array if(in_array($_POST['username'],$names)){ die("Name is a clan member name!"); }else{ //its all good! } "explode("\rn"" is for a new line??? I'm still a PHP newbie but \rn is a new line isn't it?? Just curious here!! Quote Link to comment https://forums.phpfreaks.com/topic/115435-solved-foreach-in-a-text-file/#findComment-593594 Share on other sites More sharing options...
wildteen88 Posted July 18, 2008 Share Posted July 18, 2008 If you have usernames on separate lines within a text file, use file() instead: $names = array_map('trim', file('names.txt')); if(in_array($_POST['username'], $names)){ die("Name is a clan member name!"); }else{ //its all good! } Quote Link to comment https://forums.phpfreaks.com/topic/115435-solved-foreach-in-a-text-file/#findComment-593679 Share on other sites More sharing options...
fritz.fx Posted July 19, 2008 Author Share Posted July 19, 2008 ^^ Thanks Wildteen88, but that didn't work for me. I've ended up using ProjectFears code > $names = explode("||", file_get_contents("names.txt")); //put each name into an array if(in_array($_POST['username'],$names)){ die("Name is a clan member name!"); }else{ //its all good! } And arranging my names.txt file to look like this: ||name1|| ||name2|| ||and so on|| Works fine, problem solved. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/115435-solved-foreach-in-a-text-file/#findComment-594159 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.