Jump to content

[SOLVED] foreach in a text file


fritz.fx

Recommended Posts

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??

Link to comment
Share on other sites

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!
}

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

^^

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.