doudu3961 2013-09-12 22:51
浏览 92
已采纳

修改$ _POST数组中的项目

I'm using CodeIgniter to validate form data, and then posting it to a third party site using the php header() function with the $_POST key-value pairs as URL parameters. So for example:

'first_name' => 'chris' 'area_code' => '555' 'phone_number => '555-5555'

... would become 'http://thirdpartysite.com?first_name=chris&area_code=555&phone_number=555-5555'.

The most "elegant" way I can think to do this is to iterate over the $_POST array like this...

$formValues = $this->input->post(NULL, TRUE);
foreach($formValues as $key => $value) 
{
    $postURL .= $key . '=' . $value . '&';
}

The problem is that the third party site needs to take a whole phone number as one parameter; it can't take "area code" and "phone number" broken up into two pieces as I have it on my form. So what I need to do is concatenate area_code and phone_number and store it in a new variable that gets appended to the URL string.

What's the best way to do this? I was thinking of maybe adding an if, else statement to the foreach loop that would check if the key is "area_code" or "phone_number" and do the proper actions, but is there a better way to do this? Is there a native way in php or CodeIgniter to modify the $_POST array before iterating over it?

Thank you!!

  • 写回答

3条回答 默认 最新

  • doujiao1180 2013-09-12 23:00
    关注

    You can modify the $_POST array directly in the same way as any other array.

    You could do:

    $_POST['phone'] = $_POST['area_code'] . $_POST['phone_number'];
    unset($_POST['area_code']);
    unset($_POST['phone_number']);
    

    then run your existing code.

    However I it's not a good ideal to deal with user input in this way - you should really be taking only the fields you require to pass to the third party URL otherwise someone malicious could use your script to attack the third party server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用