PHP登录表单需要提交2次

标签 php html mysql

我遇到了一个问题,我无法弄清楚。我的所有其他表格都可以正常工作。但登录表单需要完成2次才能执行。

我需要输入用户名和密码,提交表单,然后刷新页面。 当我输入用户名/密码并第二次提交时,功能起作用并转到 profile.php

登录.php

    <?php
    require_once("../Style/user-navigation.php");
    require_once("../System/class.user.php");

    $user = new USER();

    if($user->is_loggedin()!=""){
    ?><script> location.replace("Home.php?action=select&id=<?php echo $userRow['id']; ?>"); </script>
    <?php   
    }

    if(isset($_POST['btn-login'])){
        $username = strip_tags($_POST['username']);
        $password = strip_tags($_POST['password']);

        if($user->doLogin($username,$password)){ ?>
        <script> location.replace("Home.php?action=select&id=<?php echo $userRow['id']; ?>"); </script>
        <?php
        }
    }

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body class="nav-md" style="background-image: url('https://zellox.com/wp-content/uploads/2016/05/background-website-%E2%80%AB1%E2%80%AC-1600x900.gif');">

        <!-- page content -->
        <div class="clearfix"></div>

        <a class="hiddenanchor" id="signin"></a>

        <div class="login_wrapper">
        <div class="animate form login_form">
          <section class="login_content">
            <form method="post">
              <h1>Login Form</h1>
              <div>
                <input type="text" class="form-control" name="username" placeholder="Username" required="" />
              </div>
              <div>
                <input type="password" class="form-control" name="password" placeholder="Password" required="" />
              </div>
              <div>
               <button type="submit" name="btn-login" class="btn btn-primary ">Log In</button>
                <a class="reset_pass" href="#">Lost your password?</a>
              </div>

              <div class="clearfix"></div>

              <div class="separator">
                <p class="change_link">New to site?
                  <a href="Register.php" class="to_register"> Create Account </a>
                </p>

                <div class="clearfix"></div>
                <br />

              </div>
            </form>
          </section>
        </div>
        </div>
      </div>
        <!-- /page content -->
  </body>
</html>

以及后端代码“class.user.php”登录功能

public function doLogin($username,$password){
        try
        {
            $stmt = $this->conn->prepare("SELECT id, username, password FROM users WHERE username=:username");
            $stmt->execute(array(':username'=>$username));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() == 1)
            {
                if($password= $userRow['password'])
                {                   
                    $_SESSION['user_session'] = $userRow['id'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

最佳答案

在使用 session 和 HTML 之前添加它:

session_start();

关于PHP登录表单需要提交2次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44908610/

相关文章:

php - 如何提高 Zend Lucene 的性能?

php - 如何使用 PHP 获取 JSON 响应?

javascript - 如何找到数据中匹配大括号的位置?

jquery - 显示 float 对象的不需要的水平滚动条 - 仅在 IE11 中

javascript - 去掉两个选项

php - 使用 PHP 中的电子邮件回复来加速表单填写提交?

php - 我希望能够根据不同表中的内容在 Post 表中搜索 Post

php - Mysql连接错误: not showing anything at all

mysql - 修改表: only allow either unique or empty

php - 使用类似 codeigniter 显示大量数据