Rotate Username & Password

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

soma56
Forum Newbie
Posts: 11
Joined: Fri Jul 10, 2009 12:12 pm

Rotate Username & Password

Post 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.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Rotate Username & Password

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Rotate Username & Password

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Rotate Username & Password

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Rotate Username & Password

Post by jackpf »

You'd get warnings from that though as $l isn't an array :P
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Rotate Username & Password

Post by andyhoneycutt »

While this is true, I choose to ignore it and declare myself the wiener! :mrgreen:
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Rotate Username & Password

Post 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.
soma56
Forum Newbie
Posts: 11
Joined: Fri Jul 10, 2009 12:12 pm

Re: Rotate Username & Password

Post 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.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Rotate Username & Password

Post 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.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Rotate Username & Password

Post 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 :)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Rotate Username & Password

Post 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 :)
soma56
Forum Newbie
Posts: 11
Joined: Fri Jul 10, 2009 12:12 pm

Re: Rotate Username & Password

Post 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.
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: Rotate Username & Password

Post by andyhoneycutt »

I'll take a look if you post up your problems here. No need for cashola.

-Andy
soma56
Forum Newbie
Posts: 11
Joined: Fri Jul 10, 2009 12:12 pm

Re: Rotate Username & Password

Post by soma56 »

Well I appreciate it. I tried to pm you but it didn't work.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Rotate Username & Password

Post by jackpf »

Just post it here :D

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