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!
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?
$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
$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.
$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...
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.
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.
jackpf wrote:You'd get warnings from that though as $l isn't an array
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.