2013-11-03 57 views
0

我已經瀏覽了大約一百次,並找不到頁面不會重定向的原因。我無法找到任何可能會丟失的空格或輸出。任何人都可以發現問題嗎?爲什麼我的網頁在登錄後不能重定向?

這裏是我的代碼:

<?php 
include_once('includes/sessions.php'); 
include_once('connections/localhost.php'); 
include_once('includes/functions.php'); 
include_once('includes/validations.php'); 
?> 
<?php 
$username = ""; 
if (isset($_POST['submit'])) { 
    $required_fields = array("username", "password"); 
    validate_there($required_fields); 
    if (empty($errors)) { 
     $username = $_POST["username"]; 
     $password = $_POST["password"]; 
     $found_user = attempt_login($username, $password); 
    if ($found_user) { 
    $_SESSION["user_id"] = $found_user["user_id"]; 
    $_SESSION["username"] = $found_user["username"]; 
    redirect_to("index.php");  
    } else { 
     $_SESSION["message"] = "Username/password not found."; 
    } 
    } 
} else { 
} 
?> 

這裏是重定向功能:

function redirect_to($new_location) { 
     header("Location: " . $new_location); 
     exit; 
    } 

下面是驗證功能:

$errors = array(); 

function fieldname_is_text($fieldname) { 
    $fieldname = str_replace("_", " ", $fieldname); 
    $fieldname = ucfirst($fieldname); 
    return $fieldname; 
} 


function has_presence($value) { 
    return isset($value) && $value !== ""; 
} 

function validate_there($required_fields) { 
    global $errors; 
    foreach($required_fields as $field) { 
    $value = trim($_POST[$field]); 
    if (!has_presence($value)) { 
     $errors[$field] = fieldname_is_text($field) . " can't be blank"; 
    } 
    } 
} 
+0

你正在得到什麼錯誤? –

+0

這並不重要,但爲什麼要關閉並重新打開PHP機箱? (即'?> <?php')。另外,您的登錄頁面的URL是否與重定向頁面的URL相同? – hargobind

+0

發佈您的代碼爲'$ found_user'和'validate_there' –

回答

1

最有可能你有任何輸出( HTML或錯誤消息)在調用標頭函數之前。在發送輸出之前,應發送所有標題。

有些輸出你看不到,但是在那裏。如空格等嘗試刪除

?> 
<?php 
+0

是的,但在哪裏?打開它們時,我看不到任何輸出或錯誤消息。 – SherwoodPro

+0

@SherwoodPro'?> <?php'這裏你有空字符串的輸出 –

+0

也檢查你包含的文件。如果它們只包含PHP,則可以安全地刪除文件末尾的?>。這樣可以防止將任何空格發送到瀏覽器。有一些討論跳過結束標記是否是好的編碼練習,但是有效的PHP –

0

嘗試此功能進行重定向。

<?php 
function redirect_to($new_location) 
{ 
    ?> 
<script type="text/javascript"> 
    window.location = '<?php echo $new_location;?>'; 
</script> 
<?php 
} 

?> 
+0

我不能'得到這個工作。它只是給我一個空白頁。不過謝謝。 :) – SherwoodPro

+0

讓你在函數中傳遞值,變量有值(頁面路徑),$ new_location ... – Zeeshan