kellz Posted November 9, 2007 Share Posted November 9, 2007 hey.. I'm making a kinda thing that people use to login to websites..like yahoo but much MUCH more crappy lol so here it is: if (isset($_POST['Submit']) ) { if (isset($_POST['name']) && isset($_POST['pass'])) { $name = $_POST['name']; $pword = $_POST['pass']; $lines = file('np.txt'); $data = array(); foreach ($lines as $line) { $data = explode(':', $line); $user = $data[0]; $pass = $data[1]; if ($user == $name && $pass == $pword) { echo 'logged in!'; } else { echo 'user name and/or password incorrect'; } unset ($data[0]); unset ($data[1]); } } } the format of the file np.txt is like this: kelsey:password user2:lalalala user3:helloworld but it all messes up and says i am logged in AND im not logged in at the same time with whatever username or password i used. But it's weird because if i leave only 1 line in the np.txt file it all works.. I guess it's the array so I tried unset (not really knowing 100% why i tried it) but I did lol and that done nothing. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 9, 2007 Share Posted November 9, 2007 Eugh, flat files. Anyways,the problem is that you cotinue to look through the loop, even after finding a correct username and password. Imagine it in your head: We loop through line by line. The first time, we check and find a matching username and password - great, so we echo 'logged in'. However, the loop keeps running. It runs again, and the user/password doesn't match - so we echo the failure. And so on. Try: <?php if (isset($_POST['Submit']) ) { if (isset($_POST['name']) && isset($_POST['pass'])) { $name = $_POST['name']; $pword = $_POST['pass']; $lines = file('np.txt'); $data = array(); $loggedin = false; //so far, we've not found a match so the person isn't logged in foreach ($lines as $line) { $data = explode(':', $line); $user = $data[0]; $pass = $data[1]; if ($user == $name && $pass == $pword){ echo 'Logged in!'; $loggedin = true;//now we've found a match - the person's login details are correct break;//break out of the foreach loop - we dont need to look any further } } if($loggedin === false){//lets check if their details were correct echo 'user name and/or password incorrect'; } } } ?> Quote Link to comment Share on other sites More sharing options...
kellz Posted November 9, 2007 Author Share Posted November 9, 2007 thx you.. it works Quote Link to comment Share on other sites More sharing options...
kellz Posted November 9, 2007 Author Share Posted November 9, 2007 ok i tried it a different way and it doesnt work and makes me mad! lol it (i thought) does the same thing but shorter: <?php if (isset($_POST['Submit']) ) { if (isset($_POST['name']) && isset($_POST['pass'])) { $name = $_POST['name']; $pword = $_POST['pass']; $lines = file('np.txt'); $data = array(); foreach ($lines as $line) { $data = explode(':', $line); $user = $data[0]; $pass = $data[1]; if ($user == $name && $pass == $pword){ echo 'Logged in!'; break;//break out of the foreach loop - we dont need to look any further } else { echo 'user name and/or password incorrect'; } } } } ?> why doesn't that work? it breaks the loop like you said... and what does "===" mean ??? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 9, 2007 Share Posted November 9, 2007 Using your current code, we run into the same problem as before. Imagine that we are the user who is second in your file. The first time the loop runs, we see the error message. The second time the loop runs, we see the logged in message, followed by the break in the loop. With this sort of method, you have to only throw an error after testing all of the data. As for ===, it is the 'identical to' comparison operator. This means it only evaluates to true if the two things being compared are both equal and of the same type. The standard == equality comparison operator checks value only. For example: <?php $var1 = 0; $var2 = '0'; if($var1 == $var2){ //TRUE 0 is the same as 0 } if($var1 ===$var2){ //FALSE whilst 0 is the same as 0, one of our variables is a string, the other an integer. } ?> The reason why i used the === operator in the code is more habit than anything. I tend to use the identical comparison operator when dealing with booleans (true/false). A standard == operator wouldn't have affected the running of the code in the example i gave. Edit: By way of explaining the need for the 'identical to' operator, see the example here on the use of the strpos() function Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.