2013-02-28 40 views
0

我正在創建基本登錄和註冊頁面,並且在完成更改密碼錶單後,我想重定向到changepassword.php?成功。重定向的頁面如果進入瀏覽器可以正常工作,但是當提交表單時,它會重新載入changepassword.php頁面而不是?success,並且不會顯示來自php代碼塊的所有內容(即表單,列右和頁腳)。下面是我的changepassword.php代碼:標題位置不起作用

<!DOCTYPE html> 
<html> 
<?php 
include ("storescripts/init.php"); 
protect_page(); 
include ("includes/overall/head.php"); 

if (empty($_POST) === false){ 
$required_fields = array('current_password','password','password_again'); 
foreach ($_POST as $key=>$value) { 
    if (empty($value) && in_array($key, $required_fields) == true) { 
     $errors[] = 'Fields marked with an asterisk are required'; 
     break 1; 
    } 
} 
if ($_POST['current_password'] === $member_data['mem_password']) { 
    if(trim($_POST['password']) !== trim($_POST['password_again'])){ 
     $errors[] = 'Your new passwords do not match'; 
    } else if (strlen($_POST['password']) <6) { 
     $errors[] = 'Your password must be at least 6 characters'; 
    } 
} else { 
    $errors[] = 'Your current password is incorrect'; 
} 
} 
?> 
<body> 
<?php include ("includes/overall/template_header.php");?> 
<div id="mainDivShort"> 
    <h1>Change Password</h1> 
    <div id="divBreak"></div> 
    <?php include ("includes/overall/column_left.php"); ?> 
    <div id="middleContent"> 
     <?php 
     if (isset($_GET['success']) && empty($_GET['success'])) { 
echo 'You have been registered successfully'; 
} else { 

    if (empty($_POST) === false && empty($errors) === true) { 
     //echo "**********************"; 
     change_password($session_mem_id, $_POST['password']); 
     header('Location: changepassword.php?success'); 
     exit(); 
    } else if (empty($errors) === false) { 
     echo output_errors($errors); 
    } 

    ?> 
     <form action="" method="post"> 
      <ul> 
       <li>Current Password*: <br> <input type="password" 
        name="current_password"> 
       </li> 
       <li>New Password*: <br> <input type="password" name="password"> 
       </li> 
       <li>Repeat New Password*: <br> <input type="password" 
        name="password_again"> 
       </li> 
       <li><input type="submit" value="Change"> 
       </li> 
      </ul> 
     </form> 
     <?php }?> 
    </div> 
    <?php include ("includes/overall/column_right.php"); ?> 
</div> 
<?php include ("includes/overall/template_footer.php");?> 
</body> 
</html> 

而只是櫃面你需要看一下修改密碼功能:

function change_password($mem_id, $password) { 
$mem_id = (int)$mem_id; 

mysql_query("UPDATE `members` SET `mem_password` = '$password' WHERE `mem_id` = $mem_id"); 
} 

的密碼更新數據庫罰款,它只是單純的不重定向到成功頁面。 在此先感謝

+0

如果您之前未做出「回顯」或「打印」,標題將起作用,因此不要在「標題」之前輸出任何內容。 – jcho360 2013-02-28 14:49:30

+0

標題需要在任何輸出之前和任何html之前輸出。 – Jrod 2013-02-28 14:50:44

+0

如果在此之前輸出任何html,則使用'header'沒有意義 – 2013-02-28 14:51:55

回答

-1

輸出任何內容後,您不能放header('Location: changepassword.php?success');。另外header重定向應該包含絕對路徑。

+2

不是downvoter,但它沒有必要使用'絕對路徑' – 2013-02-28 14:53:48

+0

@ Mr.Alien HTTP/1.1需要一個絕對的URI作爲參數»位置: – MarcinWolny 2013-02-28 15:04:56

0

標題指令必須在內容,任何內容,包含換行符,空格,html等之前出現,否則發送標題爲時已晚。只要發送1位內容,標題已經消失。

+0

好吧,但'回聲'你已經成功註冊';'只有符合以下條件才符合條件。那麼該如何佈置呢? – jhetheringt7 2013-02-28 15:47:30