在我的特定php进程结构上编码我的未设置标记的位置?

时间:2014-10-01 17:11:07

标签: php forms

你好我的表格编码完成后包括验证和内容,因为现在我们都希望阻止用户或垃圾邮件多次提交我们的表单。所以我想在我的脚本中包含隐藏的令牌方法,但我不知道在哪里编码我的

unset( $_SESSION['form_token']);  or what i need in my process.php 

从我的form.php开始:

<?php

        session_start();
        $form_token = uniqid();
        $_SESSION['form_token'] = $form_token;
?>

和我隐藏的tokken输入:

<input type="hidden" name="form_token" value="<?php echo $form_token; ?>" />

然而我遇到麻烦的部分我的表格中有3个无线电,确定下一页后我在这里提交了代码:proces.php代码:

<?php

require_once('formvalidator.php');
  if(isset($_POST['form_btn'])) {
     $validator = new simple_fv;

        // fields info container
        $fields = array();

        // fill the container with fields data
        $fields[] = array('index'=>'name', 'label'=>'Name', 'required'=>true, 'max_len'=>25);
        $fields[] = array('index'=>'surname', 'label'=>'surname', 'required'=>true, 'max_len'=>30);
         $indexes[] = array('index'=>'email', 'label'=>'E-mail', 'required'=>true, 'type'=>'email', 'max_len'=>200);
          $fields[] = array('index'=>'phone', 'label'=>'phone number',   'min_len'=>4);
          $fields[] = array('index'=>'country', 'label'=>'Country');

        // get errors
        $error = $validator->getErrors();

        // if errors is not FALSE - print the succesfull message

    if($error) {echo $error;} 
    else {if( isset($_POST['name']) )


    $emotion = $_POST['emotion']; 

    if($emotion == 'Basic Pack') { 
    session_start(); 
    $_SESSION['form_token'] = true; 
    header('Location: /new/basicc.php'); 
    } elseif($emotion == 'Deluxe Pack') { 
    header('Location: html6.php'); 
    } elseif($emotion == 'Premium Pack') { 
    header('Location: html7.php'); 
    }

这只是一个缩短的版本,但我不知道在哪里编码unset( $_SESSION['form_token']);

 elseif($_POST['form_token'] != $_SESSION['form_token'])
        {
                $message = 'Access denied';
        }

所以我的想法就是用这个来防止用户在提交表单后返回并填写所有字段,以便将它们定向到错误或form.php但是使用干净的字段集提前谢谢

1 个答案:

答案 0 :(得分:1)

你真的不需要取消设置会话变量,因为在这种情况下你永远不会实例化它,除非有一个有效的令牌:

if $_POST['form_token'] { session_start(); $_SESSION['form_token'] = true; } else { echo 'Access Denied!'; }

如果您的脚本重定向到其他表单页面之一,请在允许它们继续之前检查$_SESSION