LemonInflux Posted August 29, 2007 Share Posted August 29, 2007 Basically, for a small project I'm doing, I wanted a flatfile log in code for entry. So, I set up this. The problem is, in index.htm (the first code), when I press submit, even if the values are right, it just says 'login failed' on login.php (the second code). Here are the codes: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css"> <!-- body,td,th { font-family: Verdana, Arial, Helvetica, sans-serif; } --> </style></head> <body> <center> <table border="0" cellspacing="5" cellpadding="5"> <form action="login.php" method="POST"> <tr> <td>Username</td> <td><input type="text" size="10" name="f_user"></td> </tr> <tr> <td>Password</td> <td><input type="password" size="10" name="f_pass"></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="submit" value="Log In"> </td> </tr> </form> </table> <p>Not a member? (<a href="register.php">register</a>)</p> </center> </body> </html> <?php // authenticate using form variables $status = authenticate($f_user, $f_pass); // if user/pass combination is correct if ($status == 1) { // initiate a session session_start(); // register some session variables session_register("SESSION"); // including the username session_register("SESSION_UNAME"); $SESSION_UNAME = $f_user; // redirect to protected page header("Location: secure_page.php"); exit(); } else // user/pass check failed { // redirect to error page echo "Login failed!"; exit(); } // authenticate username/password against usersdb // returns: -1 if user does not exist // 0 if user exists but password is incorrect // 1 if username and password are correct function authenticate($user, $pass) { $result = -1; // make sure that the script has permission to read this file! $data = file("userdb.txt"); // iterate through file foreach ($data as $line) { $arr = explode(" | ", $line); // if username matches // test password if ($arr[1] == $_POST['f_user']) { // if match, user/pass combination is correct // return 1 if ($arr[2] == md5($_POST['f_pass'])) { $result = 1; break; } // otherwise return 0 else { $result = 0; break; } } } // return value return $result; } ?> Link to comment https://forums.phpfreaks.com/topic/67228-solved-flat-php-login-not-working/ Share on other sites More sharing options...
LemonInflux Posted August 29, 2007 Author Share Posted August 29, 2007 bump Link to comment https://forums.phpfreaks.com/topic/67228-solved-flat-php-login-not-working/#findComment-337231 Share on other sites More sharing options...
lightningstrike Posted August 29, 2007 Share Posted August 29, 2007 Refrain from using session_register it is deprecated instead use $_SESSION["variablename"] = "value"; Should $arr[1] and $arr[2] be $arr[0] and $arr[1] as after exploding element keys start at 0. If that doesn't fix it, please show us what your userdb file looks like in structure. Link to comment https://forums.phpfreaks.com/topic/67228-solved-flat-php-login-not-working/#findComment-337232 Share on other sites More sharing options...
LemonInflux Posted August 29, 2007 Author Share Posted August 29, 2007 Success! thanks, it was the array thing. Link to comment https://forums.phpfreaks.com/topic/67228-solved-flat-php-login-not-working/#findComment-337275 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.