2013-07-15 56 views
0

我我的註冊頁面上獲取此錯誤:警告:不能更改頭信息

Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/signup/index.php:60) in /Applications/XAMPP/xamppfiles/htdocs/signup/index.php on line 75 

這裏是我的代碼,以便您可以看看:

<?php 

include '../core/init.php'; 
logged_in_redirect(); 

if (empty($_POST) === false) { 
    $required_fields = array('username', 'password', 'first_name', 'last_name', 'email'); 

    foreach ($_POST as $key => $value) { 
     if(empty($value) && in_array($key, $required_fields) === true) { 
      $errors[] = 'All fields are required'; 
      break 1; 
     } 
    } 

    if (empty($errors) === true) { 
     if (user_exists($_POST['username']) === true) { 
      $errors [] = 'Sorry, the username \'' . $_POST['username'] . '\' is already taken.'; 
     } 
     if (preg_match("/\\s/", $_POST['username']) == true) { 
      $errors[] = 'Your username must not contain any spaces'; 
     } 
     if (strlen($_POST['password']) < 5) { 
      $errors[] = 'Your password must be at least 5 characters'; 
     } 
     if (email_exists($_POST['email']) === true) { 
     $errors [] = 'Sorry, the email \'' . $_POST['email'] . '\' is already in use.'; 

     } 
    } 
} 


?> 

<!DOCTYPE HTML> 
<html> 
<head> 
     <title> Sem titulo </title> 
    <link href="./resets.css" rel="stylesheet" type="text/css" /> 
    <link href="./style.css" rel="stylesheet" type="text/css" /> 
    <script src="js/ajax.js"></script> 
    <script src="js/main.js"></script> 

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

</head> 

<body> 
<div class="wrapper"> 
    <div class="content"> 
     <div class="header"> 
      <div class="logo"><a href="./index.html"><img src="./images/ogo.png" border="0" /></a></div> 
      <div class="login">Já tens login? <a href="../login"><span>Login </span></a></div> 
     </div> 

     <div class="intro">    
       <div class="title"> Junta-te a nós! </div> 

<?php 

if (isset($_GET['success']) && empty($_GET['success'])) { 
    echo 'You\'ve been successfully registered'; 
} else { 

if (empty($_POST) === false && empty($errors) === true) { 
    $register_data = array(
     'username'  => $_POST['username'], 
     'password'  => $_POST['password'], 
     'first_name'  => $_POST['first_name'], 
     'last_name'  => $_POST['last_name'], 
     'email'   => $_POST['email'] 
     ); 
    register_user($register_data); 
    header ("Location: index.php?success"); 

    exit(); 

} else if (empty($errors) === false) { 
    echo output_errors($errors); 
} 


?> 
<br> 
<br> 
    <form method='post' action='' >  

    <input type="text" class="sign-up-input" placeholder="Primeiro Nome" name="first_name" required pattern="^[a-z,A-Z]+$+~"><br> 
    <input type="text" class="sign-up-input" placeholder="Último Nome" name="last_name" required pattern="^[a-z,A-Z]+$"><br> 
    <input type="text" class="sign-up-input" placeholder="O teu Email" name="email" required pattern="([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-z,A-Z]{2,4}))"><br> 
    <input type="text" class="sign-up-input" placeholder="Username" name="username" required><br> 
    <input type="password" class="sign-up-input" placeholder="Password" name="password" MinimumLength="6" required pattern="^[\s\S]{6,256}$"><br> 
    <input type="submit" value="Regista-te!" class="sign-up-button"> 

    </form> 
<?php 
} 
?> 
<br> 

    </div> 

     </div> 
</div> 



     <div class="footer"> 
      <?php include '../core/include/copyright.php'; ?> 
       <ul> 
        <li><a href="">Contacto </a></li> 
        <li><a href="./businesses/index.html">Informação </a></li> 
        <li><a href="./privacy/index.html">Politica de Privacidade </a> </li> 
        <li><a target="_blank" href="#">Termos e Condições </a></li> 
        <li><a target="_blank" href="">Empregos</a></li> 
       </ul> 
     </div> 


</html> 

是否有人可以幫助我?在此先感謝

+1

快速回答刪除:行'header(「location:index.php?success」);'回答很長,學習'不能修改頭文件'錯誤 – samayo

+0

關於這段代碼:'if(isset ($ _GET [ '成功'])&&空($ _ GET [ '成功'])){'決不在GET可變信任該數據。 – TecBrat

回答

3

頁眉需要在輸出其他內容之前設置。

您發送的HTML代碼後,設置標題。

移動如果else語句if (empty($_POST) === false && empty($errors) === true) { 之前<!DOCTYPE HTML>

+0

我如何做到這一點,這樣,當用戶註冊它被重定向? –

+0

答覆更新,希望這會有所幫助。 – Sumoanand

+0

謝謝!它幫助了很多 –

0

像上面提到別的東西輸出後不能修改標題信息。你可以嘗試輸出緩衝:http://php.net/manual/en/function.ob-start.php

+1

輸出緩衝是一種選擇,但在這裏沒有必要。正如Sumoanand所建議的那樣,邏輯只是需要向上移動。 – TecBrat

相關問題