php重定向页面问题

时间:2018-05-04 07:00:05

标签: php mysql

我使用php脚本登录我的应用程序。但它不能很好地运作。我想解决这个错误。它加载相同的页面。我想显示用户名和电子邮件是否正常然后显示索引page.i在相同的登录视图中使用php代码

<?php

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

    $email = $_POST['email'];
    $pass = $_POST['password'];

    $db = new Database();
    $db->query('select email, password from user where email = :email and password = :password');
    $db->bind(':email', $email);
    $db->bind(':password', $pass);
    $db->resultset();
    print_r($db->resultset());

    if($db->rowCount() == 1){

        header('location: index.php');  
    }

}   
?>

这是我在同一档案中的登录表单

<form method="post" action="" class="form-horizontal">

              <div class="form-group">
                  <h2 style="font-size: 25px; text-align: center; font-weight: 600; margin-bottom: 30px;">LOGIN HERE</h2>
              </div>
              <div class="form-group">                  
              </div>
              <div class="form-group">
                  <label for="email">Email: </label>
                  <input type="email" class="form-control" id="email" name="email" required="require" placeholder="Enter email">   
              </div>
              <div class="form-group">
                  <label for="password">Password</label>
                  <input type="password" class="form-control" id="password" name="password" required="require" placeholder="Enter password">  
              </div>           

              <input type="submit" class="btn btn-primary" name="submit" value="Login"/>
      </form>

3 个答案:

答案 0 :(得分:1)

所以让我们说下面的php文件名为my_file.php
    

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

    $email = $_POST['email'];
    $pass = $_POST['password'];

    $db = new Database();
    $db->query('SELECT email, password FROM user WHERE email='$email' AND  password ='$pass');
    $db->bind(':email', $email);
    $db->bind(':password', $pass);
    $db->resultset();
    print_r($db->resultset());

    if($db->rowCount() == 1){

        header('location: index.php');  
    }

}   
?>

当你按下提交时你实际上没有调用它所以在行动中你必须指定以这种形式运行的php文件

<form method="post" action="my_file.php" class="form-horizontal">

          <div class="form-group">
              <h2 style="font-size: 25px; text-align: center; font-weight: 600; margin-bottom: 30px;">LOGIN HERE</h2>
          </div>
          <div class="form-group">                  
          </div>
          <div class="form-group">
              <label for="email">Email: </label>
              <input type="email" class="form-control" id="email" name="email" required="require" placeholder="Enter email">   
          </div>
          <div class="form-group">
              <label for="password">Password</label>
              <input type="password" class="form-control" id="password" name="password" required="require" placeholder="Enter password">  
          </div>           

          <input type="submit" class="btn btn-primary" name="submit" value="Login"/>
  </form>

答案 1 :(得分:0)

替换

<form method="post" action="" class="form-horizontal">

<form method="post" action="#" class="form-horizontal">

<form method="post" action="yourfilename.php" class="form-horizontal">

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="form-horizontal">

答案 2 :(得分:0)

首先,只有在数据库中找到正确且匹配的登录详细信息时,该代码才会运行

第二次,请更新此代码

'select email, password from user where email = :email and password = :password'

"SELECT email, password FROM user WHERE email = '$email' and password = '$password'"

最后header('Location: index.php');

我希望这有效......

你去......

<?php  
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Connection Query
    $db = new Database();
    // Fetch Login Data
    $email = $_POST['email'];
    $pass = $_POST['password'];
    // Login Query
    $login = "SELECT email, password FROM user WHERE email='$email' and password='$pass'";
    // Query SQL
    $req = mysqli_query($db, $login);
    // Check if Row matched is Above Zero
    if (mysqli_num_rows($req) > 0) {
        // Redirect User
        header("Location: index.php");
    }
}
?>