Faqts : Business : Programming : Shopping For You : PHP : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

141 of 154 people (92%) answered Yes
Recently 8 of 10 people (80%) answered Yes


Is there a way to post a form to more than one php script!
How can I submit an http post method within a php program?
How to submit a form to various pages and get the output in different Iframes on a single page.

Feb 4th, 2010 05:39
Jens Clasen, Joe Bloggs, Shubhankar Banerjee, Brandon Prudent, Deepak Kumar Vasudevan, chris moss,

You cannot post a form to multiple reciptients using a HTML form and a 
web browser. To achieve this goal, you will need create a script 
receiving the form input and passing it on to the receiving hosts.
There are two possibilities:
The first thing you need to know is the transfer-method you want to use
for your script:
1. GET:
   As long as you have the URL_fopen_wrapper enabled in your 
   PHP-configuration you can simply use:
   PHP allows for you to simply open any URL and pass parameters as if 
   it was a local file. 
2. POST:
   Here you'll find two ways of submitting a POST-request:
   o the cURL-Library provides a simple interface to emulate a browser:
     (see http://www.php.net/curl and the curl-manpage)
       function HTTP_Post($URL,$data, $referrer="") {
         // parsing the given URL
         // Building referer
         if($referer=="") // if not given use this script as referer
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL,$URL_Info["host"].$URL_Info
         curl_setopt($ch, CURLOPT_HEADER,1);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
         curl_setopt($ch, CURLOPT_REFERER, $referer );
         curl_setopt($ch, CURLOPT_POST,1);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
         $result=curl_exec ($ch);
         curl_close ($ch);
         return $result;
    o since the cUrl-Library is an Extension the other way to submit
      a POST-request is doing so by hand:
      function HTTP_Post($URL,$data, $referrer="") {
        // parsing the given URL
        // Building referrer
        if($referrer=="") // if not given use this script as referrer
        // making string from $data 
        // Find out which port is needed - if not given use standard 
        // building POST-request:
        $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
        $request.="Host: ".$URL_Info["host"]."\n";
        $request.="Referer: $referrer\n";
        $request.="Content-type: application/x-www-form-urlencoded\n";
        $request.="Content-length: ".strlen($data_string)."\n";
        $request.="Connection: close\n";
        $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
        fputs($fp, $request);
        while(!feof($fp)) {
            $result .= fgets($fp, 128);
        return $result;
    Note: The third parameter is thought for making a recipient think
          the values came frome somebody else then they actually do!
          A common way to prevent other peopple doing lazy things with
          a web form is to check the referrer - though this does not
          help in every case as you can see.