Solved

php http_post question

Posted on 2003-11-10
47
847 Views
Last Modified: 2013-12-12
Hi
   i have a php form with about 10 input fields...what i need to happen is when th user clicks submit, it needs to post the variables to a remote web server running php too where they have pretty much the same form on the remote server. So i need to post the variables that will automatically get processed on the remote server. Can someone show me ho o do this..code snippet will be great...
0
Comment
Question by:hegde123
  • 24
  • 14
  • 8
  • +1
47 Comments
 
LVL 3

Expert Comment

by:red010knight
ID: 9720157
Are you trying to create a macro form submission or something of that variety? Because that is what it sounds like.

As to how to do it... You need to know is to create a page with:
<?php
header(location: "www.gohere.com/fillThisForm.php");
?>

And your done;-)
This way their are not 30 sites trying to use the same form;-)

Hope this helps,
Red010Knight
0
 

Author Comment

by:hegde123
ID: 9720220
i basically need to know how to do a http_post from a  PHP form to a remote server , how to login and send the variables etc etc
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9720309
First off Forms are not PHP, the pages that process the forms give you PHP code. But Forms in general are strictly HTML and enhanced through use of CSS, Javascript and other clientside scripting.

As for logging in to a remote server - do you:
a>Have a username?
b>Have a password?
c>know the files on the remote server to call in order to have them process your data?
d>know the variable names that the remote host is expecting?
e>have permission of the remote hosts to do what you ask?

f> have the remote interest in closing any of your open questions? Currently you have 8 open. Until you have reduced the number of those questions down considerably by either, 1>going to the customer support forum and asking for something to be done about the question- whether it is to delete or something special or  2>accepting answers so that they can be closed.


You will get no more help from me and likely no one else either.

Red010Knight

PS if you answered No to any of the above questions - seek a solution/resolution to them before making another post - Thanks!!
0
 

Author Comment

by:hegde123
ID: 9720991
a>Have a username?--yes i have a username
b>Have a password?--yes
c>know the files on the remote server to call in order to have them process your data?---yes
d>know the variable names that the remote host is expecting?---yes
e>have permission of the remote hosts to do what you ask?--yes

f> i've graded all the questions..the reason it was open was cause most of them did'nt answer my questions 100% and i got the answers elsewhere..i've still graded them anyway for the good feedback...

coming back to the question, what i'm after is the code or a resource that allows me the follwoing...post variables from my php form to the remote server with the login, pasword etc using the HTTP POST method when the user fills up the input fields on my server and hits submit..thanks in advance..i'm running php 4.3.2 on windows 2000...thanks
0
 
LVL 5

Expert Comment

by:Karitz
ID: 9721079
There are nice tutorials on how to start working with forms on the net. Please look at them and should there be any problem contact me.

http://www.evolt.org/article/User_Friendly_Forms_in_PHP/20/60144/
http://codewalkers.com/tutorials.php?show=12

KaRitZ
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9721103
how does the login system work on the remote system? to post to another server you simply need the form to point to the server's processing script with the action as their file e.g. <form method="POST" action="http://www.server.com/folder/file.php">

loz
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9723019
Then you should not have a problem just having the form be posted to the remote server and let their processing scripts handle the data.

As PHP has no real say in how things are posted. It only affects pages that are generated.

If you have to have a username and password to post data - can you tell us how the remote server is handling those? Via cookies/sessions/post/get data?

If via post - get a generic username and password and set them up in
<input type="hidden" name="username" value="blah">
<input type="hidden" name="password" value="blah">

if via get then you will need to have it :
<form method="POST" action="http://www.server.com/folder/file.php?username=blah&password=blah">

if via sessions someone else will need to help you as I have not bothered with them.

if via cookies you will need to set a cookie on your forms page
www.php.net and search for setcookie() and that should get you going.

Hope that helps,
Red010Knight
0
 

Author Comment

by:hegde123
ID: 9726603
so where does the http_post class method come i?..is that the same as the form post method....apologies for being ignorant but i'm not sure what the http_post method does..
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9726658
<form method="POST"> means send the data over an HTTP POST connection so yes you should use that

loz
0
 

Author Comment

by:hegde123
ID: 9726953
and with respect to passing the variables, do we pass the variables the same way as we pass when we post variables from one form to another?
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9726963
yes everything's exactly the same as if it were on your server, just the action points to the remote script

loz
0
 

Author Comment

by:hegde123
ID: 9727000
how do these 2 sites below differ from the normal form post method?...are they doing the same?

http://www.phpbuilder.com/mail/php-general/2003022/1806.php

http://docs.akbkhome.com/pearcvs/HTTP_Post.html
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9727135
the only difference is that they're not letting the user enter data into the form before sending off the data - the post data is sent by a script with the post data defined by the script writer whereas the post data is sent from user input using a form

loz
0
 

Author Comment

by:hegde123
ID: 9729999
how do we handle logging in to the remore server though?
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9730957
How is the remote server admin handleing login??? If you do know how he is handling logins, please post that here so that we can addequately answer your question.

Is it with PHP, MYSQL, or something else??
I believe I addressed several ways of doing it in my previous comment. If none of them are working, you will need to contact the remote server admin and ask what it is you need to do. Because there are likely security measures in place that prevent any joe smoh from doing what you are asking.

Red010Knight
0
 

Author Comment

by:hegde123
ID: 9736660
basically what they've said is that we dont need to login etc ...all i need to do is the post the variables to the form running php on the remote server...i'm in the process of trying that now..once i post the variables, how do i get it automatically processed as soon as i post ?....what i mean is have whatever action is performed on the remote server when the submit button is hit on their server?
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9736716
well if you send it to the right URL, there will be some sort of check there by the script for a variable which will set off the processing, presumably.. something like:

if($_POST["submit"]) {
  # form is submitted, process it
}

loz
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9736739
First off,
Do not try posting to their form, look at the forms action ... then use that as your form action and make sure the variable names are the same.

If their form action is something like "../IAm/here.php" then add the rest of the URL in the browser to the spot where .. is and it should work with no problem.

Red010Knight
0
 

Author Comment

by:hegde123
ID: 9769789
how do i run something on hitting submit on my server too?..what i mean is when i hit submit, the forms action will point to the action of the remote server..at the same time , i'd like to run something on our database too..is that possible
0
 
LVL 3

Expert Comment

by:red010knight
ID: 9772154
<form action="submit to here" onSubmit="do this">
</form>
Very simple
0
 

Author Comment

by:hegde123
ID: 9774430
but the action part would be posting to the remote server when submit is hit...

<form name="form1" method="post" action="remote_server.php?UID=06a4f23c5b8464a3a7259c617294bd47">..

at the same time i wanna make it run something on the local server..

basically i dont want the form page to redirect to the remote server page on submit..but just post the variables and get them processed on the remote server and redirect to the local server and run a process locally..

0
 
LVL 3

Expert Comment

by:red010knight
ID: 9775749
Look into javascript.

You wouldn't have an action. Just a your submit button having an 'onClick' attribute.
The attribute would then need to call a javascript function that sends the variables out for you while nothing changes on the page you are on.

Basically just having <form name="blah">
with a button <input type="button" value="Submit" name="submit" onclick="javascript:sendStuff();">

That should do what you want.

Red010Knight
0
 

Author Comment

by:hegde123
ID: 9775867
so in the send stuff, i can run  a process that would post the form variables to the remote server and at the same time run  a process on my database...for example on submit what i want to do it post the variables to the remote server and then insert the  variables into the mysql database on my local server....

i'm not sure how to do a post in javascript though..just know how to do it using the normal form action posting....thanks
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:hegde123
ID: 9775994
i understand that i can do something like this:

<form method="post" name="form1" action="http://remoteserver" onsubmit="submitcheck();">

but i'm still not sure that once the posting is don to the remote server, how do i run a database insert on my server...obviously cant be done within the submit check as its javascript and only runs on the client side...
0
 
LVL 3

Accepted Solution

by:
red010knight earned 250 total points
ID: 9776187
Actually, the best way to do what you want. Is best done using a post to your own processing file. Where you right to your database, and then take those variables and send it to the remote server. That would be my more favorable idea.

Now my current question is to verify things, is this your intent:
                                                  {remote server
Form page -> Processing page<    {database input    
                                                  {form page  ----> repeat if desired

The processing page would basically have three functions - remote server posting, local server input, and refresh the Form page with whatever updated values you need it to have...

Is this right? If it does what you want - that would be the recommended way to go about it.

Red010Knight

0
 

Author Comment

by:hegde123
ID: 9777804
yep..thats exactly what i want to do...how do i go about that..i know how to process forms locally, redirect the page with the updated values etc..manipulate and write to the database etc..but posting the variables from the database to the remote server is what i'm not sure ....so just what you said...remote server posting, local server input, and refresh the Form page with whatever updated values i need it to have..
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9778915
hi,

you probably want to use something like this function:

http://www.faqts.com/knowledge_base/view.phtml/aid/12039/fid/51

basically you have the form submit to a local page which does the database input and displays the form again if you want. it also uses fsock functions to connect to a remote server and post the data there as a normal form would

cheers,

loz
0
 

Author Comment

by:hegde123
ID: 9783352
doe this work on windows?
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9783392
ought to. there's a comment at http://uk.php.net/fsockopen which seems to suggest you might need to replace \r with \x0D\x0A, that's the only thing relating to windows, are you having problems?

loz
0
 

Author Comment

by:hegde123
ID: 9783466
yep..it just seem to hang when i try to call that function passing the parameters
0
 

Author Comment

by:hegde123
ID: 9783540
i've tried the XoD change to..and it just hangs..

function sendToHost($host,$method,$path,$data,$useragent=0)
                     {
                         // Supply a default method of GET if the one passed was empty
                         if (empty($method)) {
                             $method = 'GET';
                         }
                         $method = strtoupper($method);
                         $fp = fsockopen($host, 80);
                         if ($method == 'GET') {
                           $path .= '?' . $data;
                         }
                         fputs($fp, "$method $path HTTP/1.1\x0D\x0A");
                         fputs($fp, "Host: $host\x0D\x0A");
                         if ($useragent) {
                           fputs($fp, "User-Agent: MSIE\x0D\x0A");
                         }
                         if ($method == 'POST') {
                           fputs($fp,"Content-type: application/x-www-form-urlencoded\x0D\x0A");
                           fputs($fp, "Content-length: " . strlen($data) . "\x0D\x0A");
                         }
                         fputs($fp, "Connection: close\x0D\x0Ax0D\x0A");
                         if ($method == 'POST') {
                             fputs($fp, $data);
                         }

                         while (!feof($fp)) {
                             $buf .= fgets($fp,128);
                         }
                         fclose($fp);
                         return $buf;
                     }



0
 
LVL 13

Expert Comment

by:lozloz
ID: 9783704
how are you calling the function?

loz
0
 

Author Comment

by:hegde123
ID: 9783718
sendToHost('http://192.168.8.102','post','/hprice.php',
               'delete=Y&rid1=921');


within a php file
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9783742
i don't think it'll work but try taking the http:// off the front of the first argument for sendtohost

can i see the complex file where this functino is executed?

loz
0
 

Author Comment

by:hegde123
ID: 9783858
if ($delete == "Y")

    {
     $code = load_php_include ('33'); eval($code);
include "oracle_connect.inc.php";

    $sql1 = "DELETE FROM sub.handset_price WHERE ID='$rid1'";

         $ora_sql = OCIParse($oracle_conn,$sql1);
         ociexecute($ora_sql);

include "oracle_disconnect.inc.php";

     $code = load_php_include('13'); eval($code);
     exit;
    }


0
 
LVL 13

Expert Comment

by:lozloz
ID: 9783901
sorry i meant complete file.. so any includes that are relating to it too

loz
0
 

Author Comment

by:hegde123
ID: 9783951
ACTUALLY I'VE CHANGED IT AS I DONT NEED THE INCLUDES AT THE TOP....as they're just for creating the headers..i've included on einclude though at the bottom..

if ($delete == "Y")

    {
include "ora.inc.php";

    $sql1 = "BEGIN DELETE FROM sub.handset_price; COMMIT; END;";

         $ora_sql = OCIParse($oracle_conn,$sql1);
         ociexecute($ora_sql);

include "oracle_disconnect.inc.php";

     $code = load_php_include('13'); eval($code);
     exit;
    }




includes:

if ($authed == "Y")
    {
     update_session ($session_id,'Y',$email,$clerk_id,$sexp);
    }

     echo "<br></td>";

     echo "</table>
               </center>";

     echo "</td>";
     echo "</table>
               </center>";

         echo "</td>";
         echo "</table>
               </center>";

if (!isset($redirect))
    {
     echo footer;
    }

echo "</body>
      </html>";
     
//include_once "mysql_unconnect.inc.php";

//include "oracle_disconnect.inc.php";
exit;


function update_session ($session_id,$authed,$email,$id,$sexp)
    {
     if ($authed == "Y")
         {
          $sql = "UPDATE session
                  SET email = '$email',
                      clerk_id = '$id',
                      authed = '$authed',
                      date_expired = DATE_ADD(NOW(),INTERVAL '$sexp' MINUTE)
                  WHERE session_id = '$session_id'";

          mysql_query($sql) or die ("Couldn't execute SQL query - extend session");
         }
    }

0
 

Author Comment

by:hegde123
ID: 9783975
actually when i removed the http, it doesnt hang anymore..but still doesnt do the delete
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9784050
where's $delete coming from? is register_globals on? and wheres the sendtohost function/execution of it?

loz
0
 

Author Comment

by:hegde123
ID: 9784061
yep..$delete is the subsection withinn the php file...so when i say /hprice.php?delete=Y it gores to the section within the php file where $delete=Y and executes the deletion part..

the senttohost function is within another php file
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9784098
if register_globals is off, you'll need to use if ($_GET["delete"] == "Y") unless you're using a php version before 4.1.0, in which case it needs to be $HTTP_GET_VARS["delete"]. the same goes for other variables from the url (i assume $authed is one so it should become $_GET["authed"])

loz
0
 

Author Comment

by:hegde123
ID: 9784110
register globals are on....also from from to from it works fine...so on our intranet if i execute the hprice.php?delete=Y, it executes sucessfully..
0
 

Author Comment

by:hegde123
ID: 9784130
what is the simplest way to test if the function is working..?...
0
 
LVL 13

Assisted Solution

by:lozloz
lozloz earned 250 total points
ID: 9784195
function sendToHost($host,$method,$path,$data,$useragent=0)
                  {
                      // Supply a default method of GET if the one passed was empty
                      if (empty($method)) {
                          $method = 'GET';
                      }
                      $method = strtoupper($method);
                      $fp = fsockopen($host, 80);
                      if ($method == 'GET') {
                        $path .= '?' . $data;
                      }
                      fputs($fp, "$method $path HTTP/1.1\x0D\x0A");
                      fputs($fp, "Host: $host\x0D\x0A");
                      if ($useragent) {
                        fputs($fp, "User-Agent: MSIE\x0D\x0A");
                      }
                      if ($method == 'POST') {
                        fputs($fp,"Content-type: application/x-www-form-urlencoded\x0D\x0A");
                        fputs($fp, "Content-length: " . strlen($data) . "\x0D\x0A");
                      }
                      fputs($fp, "Connection: close\x0D\x0Ax0D\x0A");
                      if ($method == 'POST') {
                          fputs($fp, $data);
                      }

                      while (!feof($fp)) {
                          $buf .= fgets($fp,128);
                      }
                      fclose($fp);
                      return $buf;
                  }

$socket = sendToHost('192.168.8.102','post','/hprice.php','delete=Y&rid1=921');
if($socket) {
  print $socket;
} else {
  print "failed";
}

i think..

loz
0
 

Author Comment

by:hegde123
ID: 9815707
this doesnt seem to be working..is this the only way i can do secure socket or https connection programming?
0
 
LVL 13

Expert Comment

by:lozloz
ID: 9880513
whoops musta missed that reply

sorry i can't help you with that sorta stuff - did you get this working?

cheers,

loz
0
 

Author Comment

by:hegde123
ID: 9880521
yep..its working now..i've split the points betwen you and redknight....it just needed ssl support..basiclally php 4.3.4 doesnt have openssl support..its buggy..i've got php 5 runninbg and its working fine now. Thanks for yor help..btw i have another question that i've just posted..help would be appreciated.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now