2014-05-19 57 views
1

我有一個註冊腳本(稱爲「script.php」)分爲3個步驟;這是基本的結構(我已經剝離的東西出來像消毒用戶輸入,並防止其他步驟比1的直接訪問):

<?php 
$step = $_GET['step']; 

switch($step) { 
    case 1: 
     //show form - action="script.php?step=2" method="post" 
     break; 
    case 2: 
     //if user data is good show an overview of the data and show a button to go to step 3 (the button is enclosed in a form - action="script.php?step=3" and method="post") 
     //if not, show again form - action="script.php?step=2" method="post" - and display errors 
     break; 
    case 3: 
     //add user data into db 
     break; 
} 
?> 

真正的代碼:

<?php 
switch ($step) { 
case 1: 
    //display form 
    $html = <<<FORM 
     <form action="/install?step=2" method="post"> 

      <input type="text" name="username"> 
      <input type="email" name="email"> 
      <input type="password" name="password"> 
      <input type="password" name="password_repeat"> 
      <button class="next" type="submit">Next</button> 
     </form> 
FORM; 
    break; 
case 2: 
    $errors = array(); 
    if (empty($_POST['username'])||!preg_match_all('/^([A-Za-z0-9]+){1,16}$/', $_POST['username'])) { 
     $errors[] = 'bad/empty username'; 
    } 
    if (empty($_POST['email'])||!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { 
     $errors[] = 'bad/empty email'; 
    } 
    if (empty($_POST['password'])) { 
     $errors[] = 'empty password'; 
    } 
    if (empty($_POST['password_repeat'])) { 
     $errors[] = 'empty password confirm'; 
    } 
    if ((!empty($_POST['password'])&&!empty($_POST['password_repeat']))&&$_POST['password']!==$_POST['password_repeat']) { 
     $errors[] = 'passwords do not match'; 
    } 

    if(count($errors)>0) { 
     $error_html = 'some errors occurred'; 
     foreach ($errors as $err) { 
      $error_html .= 'error: '.$err; 
     } 
     $form_html = <<<FORM 
      <form action="/install?step=2" method="post"> 

       <input type="text" name="username" value="{$_POST['username']}"> 
       <input type="email" name="email" id="email" value="{$_POST['email']}"> 
       <input type="password" name="password"> 
       <input type="password" name="password_repeat"> 
       <button class="next" type="submit">Next</button> 
      </form> 
FORM; 
     $html = $error_html.$form_html; 
    } 
    else { 
     $ent = 'htmlentities'; 
     $html = <<<DATA_OK 
      <h3>Data overview</h3> 
      <table> 
       <tr> 
        <th>Username:</th> 
        <td>{$ent($_POST['username'])}</td> 
       </tr> 
       <tr> 
        <th>email:</th> 
        <td>{$ent($_POST['email'])}</td> 
       </tr> 
       <tr> 
        <th>Password:</th> 
        <td>{$ent($_POST['password'])}</td> 
       </tr> 
      </table> 

      <form action="/install?step=3" method="post"> 
       <button class="next" type="submit">Avanti</button> 
      </form> 
DATA_OK; 
    } 
    break; 
case 3: 
    //connect to db and insert data 
    break; 
} 
?> 
<!doctype HTML> 
<html> 
<head> 
    <title>Script</title> 
    <meta charset="utf-8"> 
</head> 
<body> 
    <?php echo $html; ?> 
</body> 
</html> 

的問題是,當我去到第3步$ _POST總是空的。第2步顯示的按鈕(如果用戶數據是好的)覆蓋$ _POST?或者是因爲該表單沒有輸入而只是提交而清空?如何將$ _POST數據傳遞給第3步而不使用隱藏字段(因爲它們將包含密碼/密碼哈希)?

我已經搜索谷歌和這裏所以但我找不到任何與我的問題有關。

+0

你爲什麼要從'$ _GET'中取值? – celeriko

+0

請發表格式HTML。你的表單是否有method =「post」? – Thomas

+1

@Joe是的,我正在休息。我會將它們添加到代碼中,我認爲它們不相關。 – plumbe0

回答

0
<form action="/install?step=3" method="post"> 
    <button class="next" type="submit">Avanti</button> 
</form> 

您希望在步驟3中發佈什麼值?沒有任何形式。即使按鈕沒有name屬性。具有名稱屬性的表單中的任何字段都不會發送EMPTY郵件。

POST數據的處理方式與隱藏字段相同。帖子的人可以看到他發佈的內容,所以沒有理由將其隱藏起來。如果他從步驟1發佈密碼,他知道他發佈了什麼。你可以散列它,並將其設置爲會話/隱藏字段,甚至認爲這是不好的做法。

我真的不明白爲什麼你有2個步驟。如果第2步只有一個按鈕,爲什麼你有它?您可以在步驟1中進行驗證,如果沒有問題,則轉到步驟3,如果沒有,則保留。

+0

所以這個表單就像我想的那樣覆蓋了我的$ _POST。在步驟2中的數據檢查通過後,如何傳遞數組?在那個階段它不應該是空的,不是? – plumbe0

+0

@pinetreesarecool序列化,會話或隱藏字段。數據不會留在內存中。這不像桌面應用程序在打開時一直使用你的內存。每個對新頁面或同一頁面的請求都以與關閉應用程序並再次打開應用程序相同的方式清除內存。我擴大了我的答案。 –

+0

謝謝你的回答。我有兩個(實際上是三個)步驟,因爲流程應該是: step1:用戶輸入 - >步驟2:輸入很好,顯示一個概述。如果用戶想繼續 - >步驟3:將數據添加到數據庫 – plumbe0