faqts : Computers : Programming : Languages : 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 153 people (92%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

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.

Dec 25th, 2005 13:34
Shubhankar Banerjee, Brandon Prudent, Jens Clasen, Deepak Kumar Vasudevan, chris moss,


You can't post a form to several PHP-scripts using the Browser.
For this purpose you will need to put a third script in between dealing
with the incoming data.
In case you want to display the output of both scripts or in case you
have access to both of them so that you can force them not to produce
any output I strongly recommend that you just include both of them.
In any other case there are two possibilities:
The first thing You need to know is the transpher-method you want to use
for your script:
Note: Don't expect that scripts allowing GET and POST will keep doing 
      so for all the time. For security reasons it is strongly 
      recommended to use $_GET and $_POST to retrieve values posted by 
      HTML-forms and to turn global_vars off (as it is deefault for PHP
      versions >4.1.0). This means that values being passed to a script
      are not availlable in global scope any longer and therfor GET and
      POST will become very different things in near future.
1. GET:
   As long as you have the URL_fopen_wrapper enabled in your 
   PHP-configuration you can simply use:
   <?php
$output1=file("http://www.server.com/index.php?".$_SERVER
["QUERY_STRING"]);
$output1=file("http://www.server2.com/index.php?".$_SERVER
["QUERY_STRING"]);
   ?>
   Meaning you can simply open any URL and pass parameters as if it 
   were 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)
     <?php
       function HTTP_Post($URL,$data, $referrer="") {
         // parsing the given URL
         $URL_Info=parse_url($URL);
         // Building referer
         if($referer=="") // if not given use this script as referer
           $referer=$_SERVER["SCRIPT_URI"];
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL,$URL_Info["host"].$URL_Info
["path"]);
         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;
       }
       $output1=HTTP_Post("http://www.server1.com/script1.php",$_POST);
       $output2=HTTP_Post("http://www.server2.com/script2.php",$_POST);
     ?>  
    o since the cUrl-Library is an Extension the other way to submit
      a POST-request is doing so by hand:
      <?php
      function HTTP_Post($URL,$data, $referrer="") {
        // parsing the given URL
        $URL_Info=parse_url($URL);
        // Building referrer
        if($referrer=="") // if not given use this script as referrer
          $referrer=$_SERVER["SCRIPT_URI"];
        // making string from $data 
        foreach($data as $key=>$value)
          $values[]="$key=".urlencode($value);
        $data_string=implode("&",$values);
        // Find out which port is needed - if not given use standard 
(=80)
        if(!isset($URL_Info["port"]))
          $URL_Info["port"]=80;
        // 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";
        $request.="\n";
        $request.=$data_string."\n";
        $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
        fputs($fp, $request);
        while(!feof($fp)) {
            $result .= fgets($fp, 128);
        }
        fclose($fp);
        return $result;
      }
      $output1=HTTP_Post("http://www.server1.com/script1.php",$_POST);
      $output2=HTTP_Post("http://www.server2.com/script2.php",$_POST);
      ?>
    Note: The third parameter is thought for making recipient think
          the values came frome somebody else then they actually do!
          A common way to prevent other peopple doing lazy thing with
          one's form is to check the referrer - though this does not
          help in every case as you can see.
P.S.: Please excuse my bad english - I'm no native speaker...
 ...    what is still being investigsted is how do we get the output of 
the several post methods on different Iframes on the same page !