3

I'm a bit confused. How do I POST data to a URL and then redirect the user's browser to that location - all in one operation?

I see

header('Location: page.php?' . http_build_query($_POST)); 

but that is GET, not POST and ppl think thats really bad practice - PHP open another webpage with POST data (why?)

My kludgy workflow involves setting up a form and then submitting it via javascript - anything has to be better than that...

I think I can do a set of header() stmts but this action happens for the user way after the page has been geenrated, so i dont think that would work

Community
  • 1
  • 1
siliconpi
  • 8,105
  • 18
  • 69
  • 107
  • 4
    Why don't you just do the POST directly to the page you need to send the user to? – Rasika Apr 05 '11 at 01:23
  • @rasika, yes - thats what i'm trying to do. Its not a form. The receving script needs data posted to it. if i have display an html form on my page , and submit it to that location, it works fine - why cant i do the same directly in php?? @deceze @end_Newbie_Dev ? – siliconpi Apr 05 '11 at 01:31

2 Answers2

2

You cannot redirect POST requests. As simple as that. Any redirect will always turn into a GET request.

If you want to receive POST data, then send that data to another page, you have two choices:

  • if both pages are on the same server, use sessions to save the data server-side, don't make the client carry it over
  • if the destination is on another server and you need to send the client there together with the data, set up another intermediate form like you are
deceze
  • 510,633
  • 85
  • 743
  • 889
  • so whats the best wayto do this. i've got a page with links displaying n what i need is when the user clicks link 1 - information is posted to a url (elsewhere) and the user's browser is redirected to it. when he clicks link 2, a different set of information is posted to the same url), and he is redirected to the same url as well. did you get what i mean @deceze? – siliconpi Apr 05 '11 at 01:38
  • @Frank Is this all on the same server, are these all your sites? Or do you need to send the data to a different server? – deceze Apr 05 '11 at 01:39
  • thanks @deceze - no, the links point to someone else's server. my users need to 'go' there. I *can* do the same via a whole bunch of forms and hiding data on the html and responding on the user's clicks, but i feel there should eb a better way! – siliconpi Apr 05 '11 at 01:45
  • @Frank Then no, there's no other way. POST requests can only be initiated by the client, the server cannot instruct the client to POST data somewhere else (which security-wise is a good thing!). You can only make a form with hidden fields and ask the user to submit it, or auto-submit it via Javascript. I'd simply style this as *"User, we're gonna send you to a 3rd party site now. Please confirm this data one last time and hit Submit when you're ready, otherwise click Cancel"*, or something along these lines. – deceze Apr 05 '11 at 01:49
  • @deceze - thank you for the clear explanation. i'll roll with forms for each link, but i also need to post some secret data that i dont want to output into the htkl displayed. do you have any suggestions on how that can be done? – siliconpi Apr 05 '11 at 01:54
  • @Frank If the client needs to POST the data to another server then you'll have to give it to the client at some point. If the data is really that secret, you need to look for another way of inter-server communication. – deceze Apr 05 '11 at 01:58
  • FYI: HTTP - by spec - has POST redirects, but the user is to be asked if to POST request to the redirected location is allowed. So this requires manual interaction with the user always. Next to that, not all browsers follow the HTTP specs. – hakre Dec 23 '11 at 17:07
0

Use AJAX to save the data before you leave the page. Use the answer you get back to fire a redirection to the new url right within the current page. Don't be affraid of Javascript and ajax. Try this light AJAX library: http://www.openjs.com/scripts/jx/

Jorj
  • 2,495
  • 1
  • 19
  • 12