简体   繁体   English

登录信息页面未重定向

[英]Login Info Page Not Redirecting

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.. 该页面只是重新加载并清除字段输入(即使它是粘性表单),并停留在同一页面上,只是URL的末尾添加了?submit=Submit

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. 我没有收到任何mysql错误-在与用户名/密码组合匹配的行查询中,数据库连接正常。

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:

<?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 的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>

删除<form> ,就在<div id="table2"> ,还要删除<form> </form>

Remove <form> and </form> just before <div id="table2"> but i say you create a simple function which handles the redirects like below. <div id="table2">之前删除<form></form> ,但是我说您创建了一个简单的函数来处理如下所示的重定向。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM