views:

661

answers:

5

Hello,

I would like my page to be redirected to new url with some post data being sent to it. I don't want to use curl or fsocket because they will not redirect user to that new url.

Is there any alternative for header("Location: blahblahblah"); to send post data? I tried with document.form.submit(); and it worked but some users are facing problem with javascript.

Can any one provide alternate solution for this? Is there any php function to redirect user to new url along with some post data being sent to this url.

Thanks in advance.

A: 

What's wrong with using header("Location: $new_url")? That is performing a redirect and is the right way to do it AFTER POSTing data.

Edit: Updated to clarify that this is not how data is posted.

jathanism
You can't send a HTTP POST request in this way.
Johannes Gorset
Ya no problem with using header("... , but how can I send post data using that? It will simply redirects me to new page. ya I can post data in query parameters but I don't want to do that as I have too many parameters and data.
eHussain
A: 
function do_post_request($url, $data, $optional_headers = null)
  {
     $params = array('http' => array(
                  'method' => 'POST',
                  'content' => $data
               ));
     if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
     }
     $ctx = stream_context_create($params);
     $fp = @fopen($url, 'rb', false, $ctx);
     if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
     }
     $response = @stream_get_contents($fp);
     if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
     }
     return $response;
 }

Have a look at this link http://netevil.org/blog/2006/nov/http-post-from-php-without-curl

Louis
A: 

the canonical way to do a redirect is with javascript, right? you need to have a simple page where you :

a) generate a form with the action url being the place you want to go

b) write hidden fields to contain your post_data

c) have javascript submit your form for you on load

Igor
Ya exactly, I tried with the same you specified, but I need alternate solution on this natively in PHP (without using any kind of javascript)
eHussain
A: 

You can't mimic the behaviour of a form using PHP. You may send POST data, but not redirect the user with the same request. You'll need to do it with a form. As Igor describes, it's possible to change the expected behaviour of forms with Javascript but I'd recommend against it.

Johannes Gorset
Ya Its working with some form hidden fields and javascript, but I want alternate solution natively in php. I can't use javascript because if it may be turned off in browser then that may create big problems to my users.
eHussain
It can't be done. Is there any reason you can't use a form?
Johannes Gorset
A: 

Alternative solutions if you just need to transfer data but POST is not mandatory:

  • redirect user and transmit data via query string
  • save data in a session and redirect user
kemp