dst_u Posted August 4, 2011 Share Posted August 4, 2011 Hello, I'm trying to prepare a php page that gets data by post, processes it and should redirect the visitor to another page, to which we also send other data by post. As I can't use a simple HTML Form, I tried the fsockopen, but it only gives me back the result and doesn't GO to that page. I also tried cURL, but as far as I could see, even when using the CURLOPT_FOLLOWLOCATION option, the header received does NOT include a "Location" field and the page is not redirected. How can/should I do this? Thanks a lot. David Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/ Share on other sites More sharing options...
voip03 Posted August 4, 2011 Share Posted August 4, 2011 can u use get or post Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251676 Share on other sites More sharing options...
dst_u Posted August 4, 2011 Author Share Posted August 4, 2011 can u use get or post The whole idea is to use POST, but automatically, without an actual form submit button Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251677 Share on other sites More sharing options...
voip03 Posted August 4, 2011 Share Posted August 4, 2011 http://www.crosscode.co.uk/php/why-using-isset-post-submit-is-not-compatible-with-internet-explorer/ Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251682 Share on other sites More sharing options...
dst_u Posted August 4, 2011 Author Share Posted August 4, 2011 http://www.crosscode.co.uk/php/why-using-isset-post-submit-is-not-compatible-with-internet-explorer/ Thanks, but this is not related. It's a verification made on the receiving server, while we're looking for a procedure to launch a POST query automatically from our server to the remote server. Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251689 Share on other sites More sharing options...
voip03 Posted August 4, 2011 Share Posted August 4, 2011 are u trying to redirect one page to new page. or display the result on a same page Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251693 Share on other sites More sharing options...
dst_u Posted August 4, 2011 Author Share Posted August 4, 2011 are u trying to redirect one page to new page. or display the result on a same page redirect Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251695 Share on other sites More sharing options...
voip03 Posted August 4, 2011 Share Posted August 4, 2011 <? $config_basedir="http://address/"; $value2 = $_GET['some value from post']; header("Location:".$config_basedir."page.php?variable1=value&variable1=".$value2); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251701 Share on other sites More sharing options...
voip03 Posted August 4, 2011 Share Posted August 4, 2011 http://php.net/manual/en/function.header.php google it ' rediretc' Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251702 Share on other sites More sharing options...
dst_u Posted August 4, 2011 Author Share Posted August 4, 2011 <? $config_basedir="http://address/"; $value2 = $_GET['some value from post']; header("Location:".$config_basedir."page.php?variable1=value&variable1=".$value2); ?> That looks like a GET request. We need to send a POST and to be redirected to that page. It's not as easy as could be expected. Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251705 Share on other sites More sharing options...
voip03 Posted August 4, 2011 Share Posted August 4, 2011 When using HTML forms you can set the form method parameter to either "GET" (default) or "POST". So, which one to use? When working with GET method you can access all form variables with the $_GET array in PHP and when using POST you can access the variables using $_POST No matter which method used you can also access all variables using $_REQUEST array. When using GET all the submitted information is displayed in the address bar as part of the URL. You will see that as information shown after ? (called a query string), something like: http://domain.com/script.php?name1=value1&name2=value2 This can be useful for example where you want to be able to bookmark a page with specific query string values. However, the GET method is limited by the length of the URL (2083 characters in Internet Explorer according to Microsoft) and each of the input values must not exceed 100 chars. Obviously you also don't want to use GET when submitting sensitive information like passwords or credit card details as they would show up in the address bar. The POST method can send a lot of data (usually limited by the server settings) and should be used for every form unless the specific application benefits from the query string in the URL when using GET. No submitted data is shown in the address bar so most browser cannot correctly bookmark pages shown after a HTTP POST method. There you have it. Unless you need the query string that you get when using the GET method (and aren't effected by it's limits) you should use POST for your forms. If you are submitting any sensitive information that should not be displayed in the address bar the only way to go is with the POST method. http://www.tizag.com/phpT/postget.php Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251712 Share on other sites More sharing options...
voip03 Posted August 4, 2011 Share Posted August 4, 2011 then use header("Location:".$config_basedir."page.php?variable1=value&variable1=".$value2); Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251713 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 OP listen to the advice that has been given to you. Voip3 gave you the correct answer, the information collected from the post method is coming from the FORM, however the GET method are also HTTP VARS provided by you usually. page 1 $name = $_POST['name']; header("location: somepage.php?name=".$name); somepage.php print $_GET['name']; Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251714 Share on other sites More sharing options...
voip03 Posted August 4, 2011 Share Posted August 4, 2011 Thank you phpSensei Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251716 Share on other sites More sharing options...
dst_u Posted August 4, 2011 Author Share Posted August 4, 2011 OP listen to the advice that has been given to you. Voip3 gave you the correct answer, the information collected from the post method is coming from the FORM, however the GET method are also HTTP VARS provided by you usually. page 1 $name = $_POST['name']; header("location: somepage.php?name=".$name); somepage.php print $_GET['name']; But somepage.php is not on our server. It's a 3rd party server to which we MUST send a POST message, so we can't use a GET method. I know that with GET it would have taken 2 minutes Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1251718 Share on other sites More sharing options...
phpSensei Posted August 4, 2011 Share Posted August 4, 2011 Here's how PHP sends post to another Server, I didn't feel like typing it, so its an example from wellho <?php // Work out the data $wannasay = array ( "email" => "[email protected]", "Psotal" => "48, Spa Road (near garage)" ); $dataels = array(); foreach (array_keys($wannasay) as $thiskey) { array_push($dataels,urlencode($thiskey) ."=". urlencode($wannasay[$thiskey])); } $data = implode("&",$dataels); // work out the request $header = "POST /text.php4 HTTP/1.1\n" . "Host: www.wellho.net\n" . "Content-Type: application/x-www-form-urlencoded\n" . "Content-Length: " . strlen($data) . "\n\n" . $data . "\n"; // establish the connection and send the request $s = socket_create(AF_INET, SOCK_STREAM, 0); $z = socket_connect($s, gethostbyname("www.wellho.net"), 80); socket_write ($s, $header, strlen($header)); $header = ""; while (true) { if (strlen($c = socket_read($s, 1))) { $header .= $c; } } socket_close($s); print nl2br(htmlspecialchars($header)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1252132 Share on other sites More sharing options...
dst_u Posted August 8, 2011 Author Share Posted August 8, 2011 Here's how PHP sends post to another Server, I didn't feel like typing it, so its an example from wellho Hi, I tried this code (socket_connect function) and I get the same problem I get with fsockopen and curl_exec. Neither redirects me to that page. My issue is not only to get the "answer" of a POST query, but to actually redirect the visitor of my page to that 3rd party page, while sending that 3rd party server data that I manipulated on my page. Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1254121 Share on other sites More sharing options...
dst_u Posted August 8, 2011 Author Share Posted August 8, 2011 Finally got a solution (using JavaScript): <?php function prepare_post($param1, $param2, $param3, $param4) { ... return $post_string; } // This line should be changed to get the incoming $_POST values $form_elements = prepare_post('1', '2', '3', '4'); $inputs = ''; foreach ($form_elements as $key => $value) { $inputs .= '<input type=\'hidden\' name=\'' . $key . '\' value=\'' . $value . '\'>'; } /**/ ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Checkout </title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script type="text/javascript" language="javascript"> function submitform(){ document.getElementById('myForm').submit(); } </script> </head> <body onload="submitform()"> <form name="myForm" action="http://www.domain.com/page.html" method="post"> <?php echo $inputs ?> </form> <script type="text/javascript" language="javascript"> document.myForm.submit(); </script> Quote Link to comment https://forums.phpfreaks.com/topic/243772-php-automatic-form-action/#findComment-1254206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.