Login Info Page Not Redirecting

时间:2015-05-24 21:44:33

标签: php mysql login

I am having trouble redirecting my code to another page after a correct username/password has been submitted. After I enter the username/password and hit submit, the page does not redirect.

The page just reloads and clears the field inputs (even though it's a sticky form), and stays at the same page, except the URL adds ?submit=Submit to the end of the URL..

I am not getting any mysql errors - the database is connecting fine at the query that matches the row with the username/password combo seems to be going through.

It should be looping through the database, checking if the username/password combo exists, and redirecting to the specified page. I'm not sure what I'm not seeing / missing here that keeps the page from redirecting. Any input would be greatly appreciated.

PHP:

<?php
        define('DB_LOCATION', 'x');
        define('DB_USERNAME', 'x');
        define('DB_PASS', 'x');
        define('DB_NAME', 'x');

        $dbc = mysqli_connect(DB_LOCATION, DB_USERNAME, DB_PASS, DB_NAME)
            or die('Error connecting to database');

    $error_message= "";

    if (isset($_POST['submit'])) {

        $user_name = $_POST['user'];
        $user_password= $_POST['pass'];


        // ADD QUERY TO CHECK IF USER/PASS COMBO IS CORRECT
        if(!empty($user_name) && !empty($user_password)) {

        $query = "SELECT * FROM employees WHERE username='$user_name' and password='$user_password'";

        $result = mysqli_query($dbc, $query)
            or die ('Error querying username/password request');

            if(mysqli_num_rows($result) == 1) {

                $user_name = $row['user'];
                $user_password = $row['pass'];

                header("Location: www.mysite.com ");

            } // end if rows

            else {
                $error_message = "You were not able to log in";
            } // end else


        } // end query


    } // end isset

?>

HTML

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Login</title>
  <link type="text/css" rel="stylesheet" href="/LESSON5/5_Signup_CSS.css">

</head>
<body>
<h1>Welcome to my website!</h1>
<h2>Please login below.</h2>
<h3>Don't have an account? <a href="/LESSON5/2%20-%20CREATE%20AN%20ACCOUNT.php">Create one here.</a></h3>

<div class="formFormat" >  
<div  id="table1">
<form name =loginForm method="post" action="<?php echo $_SERVER[' PHP_SELF' ];?>">
  <table id="cssTable">
    <tr>
        <td>Username:</td><td><input type="text" id="user" name="user" value="<?php echo $user_name ?>" /></td>
    </tr>
    <tr>
        <td>Password:</td><td><input type="text" id="pass" name="pass" value="<?php echo $user_password ?>"/></td>
    </tr>
      </table>
  </form>
  </div>

  <div id="table2">
  <form>
  <table> 
  <tr>
     <td><input type="submit" name="submit"/></td>
   </tr>
   <tr>
      <td id="createAccount"><a href="/LESSON5/2%20-%20CREATE%20AN%20ACCOUNT.php">Create an account</a></td>
   </tr>
   <tr>
    <td><?php echo $error_message ?></td>
   </tr>

  </table>
</form>
  </form>
  </div>
</div> 

<?php
    mysqli_close($dbc);
?>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

Remove <form>, just before <div id="table2">, and </form> as well.

答案 1 :(得分:0)

Remove <form> and </form> just before <div id="table2"> but i say you create a simple function which handles the redirects like below.

why don't you try something like this which creates a function for the redirect.

function Redirect($url, $permanent = false)
{
    header('Location: ' . $url, true, $permanent ? 301 : 302);
    exit();
}

Redirect('Location: www.mysite.com', false);