Page 1 of 2

Rotate Username & Password

Posted: Fri Jul 10, 2009 12:31 pm
by soma56
I'm new to PHP and I must say it's quite addicting. It's my first programming language coming from a long history of HTML and CSS. In any event this is what I want to do:

Rotate the username and password fields from an uploaded .txt file. For example: If I uploaded a txt file with 3 usernames and their corresponding passwords how can I automatically rotate their respective fields in a simple SMTP script?

Code: Select all

    $smtp_username      = "grab-username-1"; // grab from the first username located in a .txt file
    $smtp_password      = "grab-password-1";        // grab the first password located in a .txt file
 
I appreciate anyone's help in advanced.

Re: Rotate Username & Password

Posted: Fri Jul 10, 2009 1:25 pm
by andyhoneycutt
Let's assume you have the following format for your text file:

Code: Select all

Username1;Password1
Username2;Password2
Username3;Password3
You could simply:

Code: Select all

 
$fp = fopen("/path/to/file","r");
while($line = fgets($fp))
{
  $line_contents = explode(';',$line);
  $username = $line_contents[0];
  $password = $line_contents[1];
  // do some stuff.
}
fclose($fp);
Is this along the lines of what you're attempting?

-Andy

Re: Rotate Username & Password

Posted: Fri Jul 10, 2009 2:17 pm
by pickle
It sounds like you want to access each email address as identified by a username/password in the file?

~andyhoneycutt has the right idea, but I think I can do it in fewer lines :twisted:

Code: Select all

$contents = file('/path/to/your/file');
foreach($contents as $line)
{
  list($username,$password) = explode(';'$line);
  //do some stuff here
}
With both mine and ~andyhoneycutt's code, you'll want to add some extra checking - to make sure the file is present, then to check if it's in the right format, etc - but what we've got is the general idea.

Re: Rotate Username & Password

Posted: Fri Jul 10, 2009 2:53 pm
by andyhoneycutt
Agreed with pickle 100%, however this needs a change for syntax:

Code: Select all

$contents = file('/path/to/your/file');
foreach($contents as $line)
{
  list($username,$password) = explode(';',$line); // add a comma between ';' and $line
  //do some stuff here
}
Hmm. Here's my lame attempt at golfing with this one...

Code: Select all

foreach(file("test.txt") as $v) $l[] = explode(';',$v);
-Andy

Re: Rotate Username & Password

Posted: Fri Jul 10, 2009 2:58 pm
by jackpf
You'd get warnings from that though as $l isn't an array :P

Re: Rotate Username & Password

Posted: Fri Jul 10, 2009 3:11 pm
by andyhoneycutt
While this is true, I choose to ignore it and declare myself the wiener! :mrgreen:

Re: Rotate Username & Password

Posted: Fri Jul 10, 2009 3:22 pm
by andyhoneycutt
McInfo appears to be correct on this one. Just tested. While I was aware of dynamic typing, I assumed jackpf was correct as a result of how annoying I find it to be in other people's code. It SHOULD have a warning associated with it. /rant.

Re: Rotate Username & Password

Posted: Sat Jul 11, 2009 9:54 am
by soma56
Wow, quite overwhelming. I'm really happy that the community here is so open to discussion. I don't have time right now to review the codes and responses but I just wanted to give out a big thanks for everyone's help and opinions so far.

I really appreciate it.

I'll review everything in the next day or so and let you guys know how I made out.

Re: Rotate Username & Password

Posted: Sat Jul 11, 2009 11:21 am
by jackpf
McInfo wrote:
jackpf wrote:You'd get warnings from that though as $l isn't an array :P
Not in PHP. PHP uses dynamic typing. You would get an error only if $l is already set to something other than array.
Oh yeah, you're right. I always thought you'd get an error like "x is not an array" or something doing that. I always declare anything that will be an array as an array before I use it.

I guess I'm more used to C style programming with datatypes.

Re: Rotate Username & Password

Posted: Sun Jul 12, 2009 12:49 pm
by andyhoneycutt
jackpf wrote:I guess I'm more used to C style programming with datatypes.
Ditto that. C taught me well to be quite verbose in my code :)

Re: Rotate Username & Password

Posted: Sun Jul 12, 2009 4:55 pm
by jackpf
Yeah. Even if you don't have to, I still reckon you should.

If you used the same variable as something else previously, it'll kill your code with a fatal error....

Better safe than sorry :)

Re: Rotate Username & Password

Posted: Thu Jul 23, 2009 1:54 pm
by soma56
I have a couple more tweaks in the code that I can't figure out. Let me know if anyone wants to make a quick buck.

Re: Rotate Username & Password

Posted: Thu Jul 23, 2009 4:33 pm
by andyhoneycutt
I'll take a look if you post up your problems here. No need for cashola.

-Andy

Re: Rotate Username & Password

Posted: Fri Jul 24, 2009 7:19 am
by soma56
Well I appreciate it. I tried to pm you but it didn't work.

Re: Rotate Username & Password

Posted: Fri Jul 24, 2009 7:23 am
by jackpf
Just post it here :D

You'd be surprised how many people might even help you for free...