Search code examples
phphtmlmysqlmysql-error-1064

Unable to access fetch dBase data. The login page keep looping back to thesame page


Each time i tried to log in through : webhost/adminlogin.php , am redirected back to thesame page. Is there any thing i forgot to add? Thank you for your help.. Here is my script below

This is my adminlogin.php

<?php session_start(); if(isset($_SESSION[ 'admin_login'])) header( 'location:admin_homepage.php'); ?>

<!DOCTYPE html>
<html>

<head>
  <noscript>
    <meta http-equiv="refresh" content="0;url=no-js.php">
  </noscript>
  <meta charset="UTF-8">
  <title>Admin Login - Online Banking</title>

  <link rel="stylesheet" href="newcss.css">
</head>
<?php include 'header.php'; ?>

<div class='content'>
  <div class="user_login">
    <form action='' method='POST'>
      <table align="center">
        <tr>
          <td><span class="caption">Admin Login</span>
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <hr>
          </td>
        </tr>
        <tr>
          <td>Username:</td>
        </tr>
        <tr>
          <td>
            <input type="text" name="uname" required>
          </td>
        </tr>
        <tr>
          <td>Password:</td>
        </tr>
        <tr>
          <td>
            <input type="password" name="pwd" required>
          </td>
        </tr>
        <tr>
          <td class="button1">
            <input type="submit" name="submitBtn" value="Log In" class="button">
          </td>
        </tr>
      </table>
    </form>
  </div>
</div>

<?php include 'footer.php'; ?>
<?php include '_inc/dbconn.php'; if(!isset($_SESSION[ 'admin_login'])){ if(isset($_REQUEST[ 'submitBtn'])){ $sql="SELECT * FROM admin WHERE id='1'" ; $result=mysql_query($sql); $rws=m ysql_fetch_array($result); $username=m ysql_real_escape_string($_REQUEST[
'uname']); $password=m ysql_real_escape_string($_REQUEST[ 'pwd']); if($username==$rws[8] && $password==$rws[9]) { $_SESSION[ 'admin_login']=1; header( 'location:admin_hompage.php'); } else header( 'location:adminlogin.php'); } } else { header(
'location:admin_hompage.php'); } ?>


Solution

  • I think there may be a few issues with your script. One obvious one is that you are outputting html content before you send the headers at the bottom of the script.

    HTTP headers must be sent to the client before any other content.

    Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

    I would suggest that unless you need to continue processing. You move all your login code to the top of the script and call 'exit()'; after you have sent the headers to the client to prevent any further processing or content being sent to the client.