TheJoey Posted October 15, 2009 Share Posted October 15, 2009 <?php $file = fopen('user.txt', 'r'); /* Set login to false initially */ $login = false /* Break out of loop if login becomes true OR EOF is encountered */ while(($login == false) && (!eof($file))) { $data = explode(":", fgets($file)); /* assume field 0 is username, field 1 is password */ if( ($data['0'] == $username) && ($data['1'] == $password) { /* login is true, so will break out of loop after this iteration */ $login = true; } } if $login = true { echo 'hi there user'; } else {echo 'nope';} fclose($file); ?> <html> <body> <form action="login4.php" method="post"> <ul> <li><label for="username"> USERNAME: </label><input type="text" name="username" id="username"/></li> <li><label for="password"> PASSWORD: </label><input type="password" name="password" id="password"/></li> </ul> <input name="login" type="submit" value="Login"/ </form> </body> </html> Parse error: syntax error, unexpected T_WHILE in C:\xampplite\htdocs\login tests\login4.php on line 8 having trouble writing a login script that uses .txt files. Link to comment https://forums.phpfreaks.com/topic/177751-solved-login-issues/ Share on other sites More sharing options...
slyte33 Posted October 15, 2009 Share Posted October 15, 2009 Your missing a ";" at line 5, add it to the end. Link to comment https://forums.phpfreaks.com/topic/177751-solved-login-issues/#findComment-937221 Share on other sites More sharing options...
TheJoey Posted October 15, 2009 Author Share Posted October 15, 2009 <?php $username = $_POST['username']; $password = $_POST['password']; $file = fopen("user.txt", "r"); /* Set login to false initially */ $login = false; /* Break out of loop if login becomes true OR EOF is encountered */ while(($login == false) && (!feof($file))) { $data = explode(":", fgets($file)); /* assume field 0 is username, field 1 is password */ if( ($data['0'] == $username) && ($data['1'] == $password)) { /* login is true, so will break out of loop after this iteration */ $login = true; } } if ($login = true) { echo 'hi there user'; } else {echo 'nope';} fclose($file); ?> <html> <body> <form action="login4.php" method="post"> <ul> <li><label for="username"> USERNAME: </label><input type="text" name="username" id="username"/></li> <li><label for="password"> PASSWORD: </label><input type="password" name="password" id="password"/></li> </ul> <input name="login" type="submit" value="Login"/ </form> </body> </html> the code is fixed, and a few other errors are now fixed althought its always displaying 'hi there user' even in the password is wrong Link to comment https://forums.phpfreaks.com/topic/177751-solved-login-issues/#findComment-937229 Share on other sites More sharing options...
Coreye Posted October 15, 2009 Share Posted October 15, 2009 Try this: <?php $username = $_POST['username']; $password = $_POST['password']; $file = fopen("user.txt", "r"); /* Set login to false initially */ $login = false; /* Break out of loop if login becomes true OR EOF is encountered */ while(($login == false) && (!feof($file))) { $data = explode(":", fgets($file)); /* assume field 0 is username, field 1 is password */ if( ($data['0'] == $username) && ($data['1'] == $password)) { /* login is true, so will break out of loop after this iteration */ $login = true; } } if ($login == true) { echo 'hi there user'; } else { echo 'nope'; } fclose($file); ?> Link to comment https://forums.phpfreaks.com/topic/177751-solved-login-issues/#findComment-937236 Share on other sites More sharing options...
TheJoey Posted October 15, 2009 Author Share Posted October 15, 2009 thank you worked a treat. may i ask what you changed. and say i wanted to register a session could i do it like this if ($login == true) { $_SESSION['login'] = true; echo 'hi there user'; } else { echo 'nope'; } Link to comment https://forums.phpfreaks.com/topic/177751-solved-login-issues/#findComment-937240 Share on other sites More sharing options...
Coreye Posted October 15, 2009 Share Posted October 15, 2009 You had if ($login = true) and I changed it to if ($login == true) And yes, that should work with sessions. Link to comment https://forums.phpfreaks.com/topic/177751-solved-login-issues/#findComment-937248 Share on other sites More sharing options...
TheJoey Posted October 15, 2009 Author Share Posted October 15, 2009 thank you so much... was going insane! Link to comment https://forums.phpfreaks.com/topic/177751-solved-login-issues/#findComment-937250 Share on other sites More sharing options...
TheJoey Posted October 15, 2009 Author Share Posted October 15, 2009 <?php if ($login == true){ $_SESSION['login'] = true; # If successfull header("location: success.php"); # redirect to this page } else { # Else redirect to header("location: unsuccess.php"); # this page } fclose($file); ?> doesnt seem to be registering the session Link to comment https://forums.phpfreaks.com/topic/177751-solved-login-issues/#findComment-937264 Share on other sites More sharing options...
dymon Posted October 15, 2009 Share Posted October 15, 2009 you should put at the beginning of the script: session_start () Link to comment https://forums.phpfreaks.com/topic/177751-solved-login-issues/#findComment-937284 Share on other sites More sharing options...
TheJoey Posted October 15, 2009 Author Share Posted October 15, 2009 ARGH thats frustrating thought i had it there Link to comment https://forums.phpfreaks.com/topic/177751-solved-login-issues/#findComment-937291 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.