dongqi19827 2010-07-23 20:52
浏览 51
已采纳

PHP将HTTP_POST_VARS发送到另一个页面?

Whats the best was of doing this?

Sessions? How can I take all my variables defined on 1 page and send them over to another.

I read using serialize convert the HTTP_POST_VARS to a string, and then pass that (using a hidden form/input?) and use unserialize on the other PHP page to get the variables back.

Also I saw someone just use something like:

<?php 
foreach($HTTP_POST_VARS as $key => $val) { 
?> 
<input type="hidden" name="<?php echo $key; ?>" value="<?php echo $val; ?>"> 
<?php 
} 
?>

Which seems ugly and asking for trouble.


Basically this is the run down of what I am trying to do:

The user fills out a form and then submits the form with all the information thats required. A second page intercepts all the HTTP_POST_VARS and then defines more variables. The user is asked if the information is correct and asked if they would like to submit all information. So far I have gotten this far. Now I want a button/link where the user clicks it and then it sends all the information page 2 has to another page where it finally runs the code to process all the information. (MYSQL, EMAILS, etc)

My ideal solution would be able to define something like onclick where I can just run a PHP function at whim, but that doesn't exist. One thing is I want to make sure information thats posted/pushed/whatever to page3 (processing) that its legit and actually comes from page2 (confirmation)...I don't want people just randomly making HTTP POSTs and having it validate. I was thinking of running some kind of MD5 stuff with a secret key to validate.

Does anyone have an elegant solution of a form where you have PART 1 ( filling out), PART 2 (confirmation to user) and PART 3 (processing all information from PART 2).

  • 写回答

4条回答 默认 最新

  • dongli5785 2010-07-23 22:27
    关注

    I would store the values in a session variable after the initial submission and then use them accordingly after confirmation/validation:

    <?php
    
    /////////////////////////////////
    // STEP 1 - Initial Form Display
    /////////////////////////////////
    
    session_start();
    echo '<form>';
    echo '<input type="text" name="usr_name" />';
    echo '<input type="text" name="usr_phone" />';
    echo '<input type="text" name="invalid_field" />';
    echo '<input type="submit" name="submit" value="Submit" />';
    echo '</form>';
    
    /////////////////////////////////
    // STEP 2 - Confirmation Page
    /////////////////////////////////
    
    // change this by your global of choice ($_POST, $_GET, $_REQUEST)
    $input_source &= $_GET;
    // create an array of all input fields that start with 'usr_'
    $input_fields = @preg_grep( '#^usr_[a-z]+#i', array_keys( $input_source ) );
    if( !empty( $input_fields ) )
    {
        // store all valid input fields in the session
        $_SESSION['input_values'] = array();
        foreach( $input_fields as $key )
        {
            $_SESSION['input_values'][$key] = $input_source[$key];
        }
    
        // create a checksum from the user's IP address and all input values (for false sense of security ^_^)
        $_SESSION['input_checksum'] = md5( $_SERVER['REMOTE_ADDR'] . '|' . join( '', $_SESSION['input_values'] ) );
    
        // logic for data validation and confirmation HTML goes here...
    }
    
    /////////////////////////////////
    // STEP 3 - Final Validation
    /////////////////////////////////
    
    // check for the existence of the session values from step 2
    if( !empty( $_SESSION['input_values'] ) && !empty( $_SESSION['input_checksum'] ) )
    {
        // create comparison checksum for validation purposes
        $_comp_checksum = md5( $_SERVER['REMOTE_ADDR'] . '|' . join( '', $_SESSION['input_values'] ) );
    
        // check session and comparisson checksums
        if( $_SESSION['input_checksum'] == $_comp_checksum )
        {
            // confirmation/validation looks good, proceed...
        }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)