Jump to content

Admin Login Script - Error


finnaz

Recommended Posts

I got this script from a php5 web developer book, but cant seem to get it to work. Can anyone see any errors or admissions in the code? When i enter the correct username and password, it says that the username / password is invalid.

 

<?php
session_start();
include "conn.inc.php";

if (isset($_POST['submit'])) {
  $query = "SELECT username, password, admin_level FROM admin " .
           "WHERE username = '" . $_POST['username'] . "' " .
           "AND password = (PASSWORD('" . $_POST['password'] . "'))";
  $result = mysql_query($query) 
    or die(mysql_error());

  $row = mysql_fetch_array($result);

  if (mysql_num_rows($result) == 1) {
    $_SESSION['admin_logged'] = $_POST['username'];
    $_SESSION['admin_password'] = $_POST['password'];
    $_SESSION['admin_level'] = $row['admin_level'];
    header ("Refresh: 5; URL=" . $_POST['redirect'] . "");
    echo "You are being redirected to your original page request!<br>";
    echo "(If your browser doesn't support this, " .
         "<a href=\"" . $_POST['redirect']. "\">click here</a>)";
  } else {
?>
<html>
<head>
<title>Administrator Login</title>
</head>
<body>
<p>
  Invalid Username and/or Password<br>
  <form action="admin_login.php" method="post">
    <input type="hidden" name="redirect" 
      value="<?php echo $_POST['redirect']; ?>">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br><br>
    <input type="submit" name="submit" value="Login">
  </form>
</p>
</body>
</html>
<?php
  }
} else {
  if (isset($_GET['redirect'])) {
    $redirect = $_GET['redirect'];
  } else {
    $redirect = "index.php";
  }
?>
<html>
<head>
<title>Beginning PHP5, Apache and MySQL</title>
</head>
<body>
<p>
  Login below by supplying your username/password...<br>
  <form action="admin_login.php" method="post">
    <input type="hidden" name="redirect" 
      value="<?php echo $redirect; ?>">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br><br>
    <input type="submit" name="submit" value="Login">
  </form>
</p>
</body>
</html>
<?php
}
?>

 

Many Thanx

Link to comment
Share on other sites

I would suggest you echo the query and verify everything is being passed correctly and also verify the info matches as it's stored in the DB.

 

Also, this line is not really being used

 

$row = mysql_fetch_array($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.