2017-03-06 41 views
0

我在php中创建一个登录页面。我想在成功登录后将用户重定向到自动页面。由于我使用pdo,因此我使用require_once来调用pdo.php。但是,这行代码阻止重定向指定的页面。如果我删除了require_once代码,我可以成功地返回到索引页后,我打cancle。下面的代码会带我到auto.php页Require_once阻止我重定向PHP

<?php 

require_once "pdo.php"; 
require_once "bootstrap.php"; 

if (isset($_POST['cancel'])) { 
    // Redirect the browser to game.php 
    header("Location: index.php"); 
    return; 
} 

$failure = false; 


if (isset($_POST['email']) && isset($_POST['password']) ) { 

     if (strlen($_POST['email']) < 1 || strlen($_POST['password']) < 1) { 
     $failure = "User name and password are required"; 
     } 


    else if (strpos($_POST['email'], '@')!== false) 
    { 

     $e = htmlentities($_POST['email']); 
     $p = htmlentities($_POST['password']); 

     $sql = "SELECT email FROM users 
     WHERE email = '$e' 
     AND password = '$p'"; 

     echo "<p>$sql</p>\n"; 

     $stmt = $pdo->query($sql); 
     $row = $stmt->fetch(PDO::FETCH_ASSOC); 

     var_dump($row); 
     echo "-->\n"; 
     if ($row === FALSE) { 
     echo "Incorrect password"; 
     } 
     else { 
     header("Location: autos.php?name=".urlencode($_POST['email'])); 
     return; 
     } 
    } 
    else 
     { 
      $failure = "Email must have an at-sign (@)"; 
     } 

} 
?> 
+0

可能因为文件不存在,所以代码停止执行。 –

+0

检查日志。什么是错误? –

+0

您是否尝试过在if(isset($ _ POST ['cancel']))'后面使用'require_once'? – DrKey

回答

0

This是一个引导问题。 bootstrap.php输出一些防止重定向的信息。我将标题标签下的require_once“bootstrap.php”移动并解决了问题。

0

检查没有输出是由你的PHP需要的文件发送,通过在结束例如空格或行,任何输出发送到布劳尔块还重定向或任何其他标题,关闭php标记不是最佳实践,尤其是因为这种问题

如果这不是问题,请检查您没有错误(解析或丢失文件),以显示它们全部将其包含在脚本顶部

error_reporting(-1); 
ini_set('display_startup_errors',true); 
ini_set('display_errors','stdout');