doumian3780 2010-05-03 01:29
浏览 133

PHP表单POST到外部URL,重定向到另一个URL

So, what I am trying to accomplish is have a self-posting PHP form, POST to an external page (using CURL) which in turn redirects to another page. Currently, what is happening is that once I click "Submit" on the form (in contact.php) it will POST to itself (as it is a self-posting form). The script then prepares the POST using CURL and performs the post. The external page does its processing and then, the external page is supposed to redirect back to another page, in a referring domain. However, what happens instead, is that it seems like the contact.php page loads the HTML from the page the external page redirected to, and then, the contact.php's HTML loads after that, ON THE SAME PAGE.

The effect, is what looks like two separate pages rendered as one page. Naturally, I just want to perform the POST and have the browser render the page it is supposed to redirect to as specified by the external page.

Here is the code I have so far:

<?php     
if(isset($_POST['submit']))
 {
  doCURLPost();
 }

 function doCURLPost()
 { 
  $emailid = "2, 4";

  $hotel = $_POST['hotel'];


  //you will need to setup an array of fields to post with
  //then create the post string

  $data = array ( "recipient" => $emailid,
      "subject" => "Hotel Contact Form",
      "redirect" => "http://www.localhost.com/thanx.htm",
      "Will be staying in hotel: " => $_POST['hotel'],
      "Name" => $_POST['Name'],
      "Phone" => $_POST['Phone'],
      "Comments" => $_POST['Comments']);

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "http://www.externallink.com/external.aspx");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_POST, true);  
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Referer: http://www.localhost.com/contact.php"));
  $output = curl_exec($ch);
  $info = curl_getinfo($ch);
  curl_close($ch);
 }
?>
  • 写回答

1条回答 默认 最新

  • duanrong3308 2010-05-03 01:34
    关注

    Seems like you're almost there.

    You probably just want to die() or exit() after displaying the stuff returned fro cURL, so that your pre-POST version of contact.php doesn't get sent to the browser.

    So you can just halt execution, or structure your code with conditionals so either the "display a form" stuff happens, or the "do some curly post and display the results" stuff happens, but not both.

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料