NoUser2U Posted December 28, 2010 Share Posted December 28, 2010 Hi all,A week or 2 ago I was busy with a very basic register/login system, where userinfo is stored in a .txt file called users.txt. The login condition was: "If the username inserts firstname and lastname, and it matches some values in the .txt file, then login is success!". Below is the code: <?php$users="C:/wamp/www/project_02_login2/files/users.txt";$fh=fopen($file, 'r');$lines=file($users, FILE_IGNORE_NEW_LINES);if(isset($_POST['firstname']) && isset($_POST['lastname'])){ // This does not work 100% $firstname=$_POST['firstname']; $lastname=$_POST['lastname']; foreach($lines as $line){ list($ln, $fn)=explode(';', $line); if($lastname==$ln && $firstname==$fn){ echo 'Login was successful!'; break; } else { echo 'Login failed!'; break; } }} else { echo 'Please enter your details below';}?> When i go to the login-page for the first time when opening my site in the browser, then it does say 'Please enter your details below'. But when i test the login-page by submitting the form while keeping the firstname and lastname fields empty, then i get "Login failed" instead of 'Please enter your details below'.What is the cause of this?Thnx in advance!In case it is necessary to know, the .txt file contains:lastname1;firstname1lastname2;firstname2lastname3;firstname3lastname4;firstname4etc. etc.That's why i used list($ln, $fn)=explode(';', $line); Link to comment Share on other sites More sharing options...
[dx] Posted December 28, 2010 Share Posted December 28, 2010 Why not use SQL, or variables in PHP?$login_1 = "login-here";$pass_1 = "pass-here";$login_2 = "login2-here";$pass_2 = "pass2-here";etc. Link to comment Share on other sites More sharing options...
jeffman Posted December 28, 2010 Share Posted December 28, 2010 A database will not answer OP's question, and I think from other posts that OP is just now learning sql.It might be worth var_dump($_POST) just to see what is being sent to the server. Link to comment Share on other sites More sharing options...
thescientist Posted December 28, 2010 Share Posted December 28, 2010 perhaps use the empty function to test for the presence of firstname and lastname. Link to comment Share on other sites More sharing options...
[dx] Posted December 28, 2010 Share Posted December 28, 2010 A database will not answer OP's question, and I think from other posts that OP is just now learning sql.It might be worth var_dump($_POST) just to see what is being sent to the server.For sure.. First edit script to work with SQL. Link to comment Share on other sites More sharing options...
NoUser2U Posted December 28, 2010 Author Share Posted December 28, 2010 A database will not answer OP's question, and I think from other posts that OP is just now learning sql.It might be worth var_dump($_POST) just to see what is being sent to the server.Thank you Deirdre!I used var_dump on $_POST and it returned: array(3) { ["firstname"]=> string(0) "" ["lastname"]=> string(0) "" ["submit"]=> string(7) "Submit!" } So obviously the $_POST's for firstname and lastname were 0 characters long.So i edited the code slightly and inserted another if() function, so it looks like this now: if(isset($_POST['submit'])){ // This now works!! $firstname=$_POST['firstname']; $lastname=$_POST['lastname']; if(strlen($firstname) == 0 || strlen($lastname) == 0){ echo 'Please enter your details below'; } else {// REST OF THE CODE AS DESCRIBED IN THE FIRSTPOST} Thank you for hinting the var_dump() function Deirdré's Dad!. I will now definately try to think about that function more when i encouter problems! Link to comment Share on other sites More sharing options...
jeffman Posted December 28, 2010 Share Posted December 28, 2010 An alternative to the isset function is the empty function. Like isset, it does not throw a warning if you pass it an unset variable. In your case it would tell you that your variables have been set but have no content. Something like:if (!empty($_POST['firstname']) && !empty($_POST['lastname']) ) {// check the login}else {// handle the mistake} Link to comment Share on other sites More sharing options...
iwato Posted December 29, 2010 Share Posted December 29, 2010 Thank you Deirdre! Thank you for hinting the var_dump() function Deirdré's Dad!. I will now definately try to think about that function more when i encouter problems! Yeah, he taught me the same many moons ago, and I have since been able to answer many questions on my own. A very handy function and helpful person, indeed.Roddy Link to comment Share on other sites More sharing options...
jeffman Posted December 29, 2010 Share Posted December 29, 2010 Shucks, guys. Anyway, I'm pretty sure I learned how useful that function is from justsomeguy (no surprise there). Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.