2017-03-06 55 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');