2017-05-25 185 views
-1

我試圖發送密碼到PHP命令。但是當我echo它檢查條件,它的工作原理。但是如果我不是echo,而是使用headers()代替,如果密碼正確,它將不會重定向。但是如果密碼不正確,它會重定向到錯誤頁面以顯示錯誤。PHP不重定向到特定頁面

這是我的代碼。

<?php 
    //require_once "condb.php"; 
?> 

<?php 
    if (isset($_POST['BTN_ENTER'])) 
    { 
    $password=$_POST['password']; 
    //echo $password; 

    if ($password == "test") 
    { 
     //echo "ok!"; 
     header('location:survey.php'); 
    }else 
    { 
     //echo "not ok!"; 
     header('location:error.php'); 
    } 
} 
?> 

<!DOCTYPE html> 

<html> 

<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <title>Login</title> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 

    <script 
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> 
    </script> 
    <script src="js/bootstrap.min.js"></script> 
</head> 

<style> 
div#index 
{ 
    height: 100px; 
    position: absolute; 
    top:0; 
    bottom: 0; 
    left: 0; 
    right: 0; 
    margin: auto; 
} 
</style> 

<body style="background-color:#4da6ff;"> 
    <div align="center" id="index" class="row col-sm-12 col-md-6 col-lg-12 col-xs-12"> 
    <p style="font-size:32px;color:#66ff33;">Please enter passcode.</p> 

    <form method="POST"> 
    <div align="center" class="row col-sm-12 col-md-12 col-lg-12 col-xs-12" 
style="margin:0 auto;"> 
     <input type="password" class="form-control input-lg" placeholder="Please enter a passcode." style="width:40%;height:50%" name="password" autofocus><br> 
    </div> 

    <button type="submit" class="btn btn-success btn-lg" name="BTN_ENTER" 
    style="height:50%;width:20%;">ตกลง 
    </button> 

    </form> 

</div> 

回答

0

你可以試試這個?

if ($password == "test") 
{ 
    header('Location: survey.php', true, 302); 
    die(); 
} 
else 
{ 
    header('Location: error.php', true, 302); 
    die(); 
} 
+0

它並沒有表現出任何錯誤。謝謝您的回答。我只是忘記了「$ _SESSION」。 – PaoPaoMC

0

如果您想要全局訪問該值,首先應使用會話或cookie。

嘗試使用JavaScript代碼段用於重定向

echo "<script>window.location='survey.php';"</script>";