2014-05-19 56 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