Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/82802-reading-text-files/
Share on other sites

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";

 

}

 

Link to comment
https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421113
Share on other sites

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".

Link to comment
https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421152
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421161
Share on other sites

     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

Link to comment
https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421175
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421507
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421660
Share on other sites

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?

 

Link to comment
https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-421669
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/82802-reading-text-files/#findComment-422340
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.