2017-02-20 117 views
0

我需要你的幫助關於我的代碼在PHP中。我正在製作簡單的登錄表單,但在登錄成功時出現錯誤。當我登錄成功時,我想重定向到另一個網頁。頁面重定向(標題)問題在PHP

瀏覽器錯誤:(請參閱附加圖片)enter image description here 本地主機頁面不工作 localhost重定向您太​​多次了。 嘗試清除您的Cookie。 ERR_TOO_MANY_REDIRECTS

<?php 

$errors = array(); 
if(isset($_POST['submit'])) { 
    $username = trim($_POST["username"]); 
    $password = trim($_POST["password"]); 

    if(!isset($username) || empty($username) || !isset($password) || empty($password)) { 
     $errors['$blank'] = "Fields can't be blank."; 
    } 

    else { 
     $errors = ""; 
    } 

    $min = 6; 
    if(strlen($username) < $min || strlen($password) < $min) { 
     $errors['$minAllowed'] = "Only minimum of 6 characters is allowed"; 
    } 

    $max = 12; 
    if(strlen($username) > $max || strlen($password) > $max) { 
     $errors['$minAllowed'] = "Only maximum of 12 characters is allowed"; 
    } 

} 

function form_errors($errors=array()) { 
    $output = ""; 
    if(!empty($errors)) { 
     $output = "<div class=\"error\">"; 
     $output .= "Please fix the following errors:"; 
     $output .= "<ul>"; 
      foreach ($errors as $key => $error) { 
       $output .= "<li>{$error}</li>"; 
      } 
     $output .= "</ul>"; 
     $output .= "</div>";  
    } 
    else { 
     header("Location: " . "p1.php"); 
    } 


    return $output; 
} 

?> 
+0

什麼是'標題的意思( 「位置:」 「p1.php」);'? – Nawin

+0

檢查您的標題位置線的其他語句 – Akintunde007

+0

'header(「Location:」。「p1.php」);'應該是'header(「Location:p1.php」);' –

回答

0

您的重定向語法是不正確的。使用這樣的:

header("Location: http://www.example.com/p1.php"); 
+2

在重定向之後總是使用'die()'!閱讀爲什麼在這裏:https://aaronsaray.com/2013/it-is-important-to-use-die-after-a-header-redirect-heres-Why –

+0

嗨,謝謝!這已經在工作了。 – studentAir02

0

使用這樣的:

function redirect_to($location) 
    { 
     if (!headers_sent()) { 
      header('Location: ' . $location); 
      exit; 
     } else { 
      echo '<script type="text/javascript">'; 
      echo 'window.location.href="' . $location . '";'; 
      echo '</script>'; 
      echo '<noscript>'; 
      echo '<meta http-equiv="refresh" content="0;url=' . $location . '" />'; 
      echo '</noscript>'; 
    }} 


redirect_to('http://www.example.com/p1.php');