Jump to content

[SOLVED] flat PHP login not working


LemonInflux

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.