PHP help comparing user data to file

ligerzer

New Member
Prime Account
Messages
2
Reaction score
0
Points
0
I'm doing an assignment for school and we have to use a PHP script to check the user name and password when they try logging in.

So I have file users.txt sitting on the server and all it contains is a list of user, 1 per line. It has the users first name, space, last name, space, and then email. Like this:
userFirst userLast name@domain.com

So for I've been able to get to this, but being new to PHP I am a bit stumped and unsure how to proceed...
$fName=$_GET['txtFirstName'];
$lName=$_GET['txtLastName'];
$email=$_GET['txtEmail'];
$lines=file("users.txt");
foreach($lines as $line_num => $line){
if ()
//no matter what i try in here, I get stuck
}

All I need to check if the 3 fields entered by the user match any one of the lines in user.txt. Users can enter lower or upper case letter for names and email but not the password, but for that part I just plan on having two compare statements for each name.

Any help with how to make the foor loop work would be greatly appreciated!
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Please use
PHP:
, [html] or [code] tags (as appropriate) to separate and format code.

There's little need to copy data from the [c]$_GET[/c] array to other variables.

Homework should be based on the material from your class, so that's all that should be needed to complete the assignment. If you didn't understand the material or the material wasn't sufficient, the first people you should turn to are the teacher and any TAs. They also have better knowledge of the requirements for the assignment. Speaking of, what are they? The description you've given thus far is underspecified in this, as with other aspects. Learning to fully specify a problem is an important (some would say the most important) skill in programming.

The system you've described is missing an important component: password storage. The flat text file doesn't include a password field. How are you planning on storing passwords?

There are any one of a number of ways of implementing this. The most direct is to use [URL="http://php.net/explode"][c]explode[/c][/URL] or [URL="http://php.net/preg_split"][c]preg_split[/c][/URL] to separate each line into three fields. [URL="http://php.net/strtolower"][c]strtolower[/c][/URL] and [URL="http://php.net/strtoupper"][c]strtoupper[/c][/URL] convert strings to lower and upper case, respectively. There are quite a few other [URL="http://php.net/strings"]string functions[/URL]. The most important thing to take away from this is that PHP has extensive documentation covering the language, standard functions and classes, and extensions. Many questions you will have can be answered by reading these manual pages.

This is a rather poor assignment, as it teaches bad lessons along with any good. Flat text files aren't scalable; they require [URL="http://www.cforcoding.com/2009/07/plain-english-explanation-of-big-o.html"]O(n)[/URL] time to access data. Passwords should never be stored as plain text (which I expect you're doing, but as that part hasn't been specified...). Using the query string to pass data ([c]$_GET[/c]) can open your site to certain types of attacks (e.g. [URL="https://www.owasp.org/index.php/Session_fixation"]session fixation[/URL]) in some circumstances; use query string data when you want to explicitly allow creating a link to the generated page (which shouldn't be the case for registration and login pages).

See also:
[list]
[*][URL="http://x10hosting.com/forums/programming-help/98898-microsoft-sql-server-rant.html#post553156"]Microsoft SQL Server (a rant)[/URL]
[*][URL="http://x10hosting.com/forums/programming-help/107441-need-experts-opinion.html#post613624"]Need experts' opinion[/URL]
[*][url=http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html]Enough With The Rainbow Tables: What You Need To Know About Secure Password Schemes[/url]
[/list]
 
Last edited:
Top