I would like to redirect to another url along with POST data. Generally I user Header('Location:desiredurl') to redirect to a url in php. With this we cannot assign any post data to the url. But I want some php syntax with which I can redirect to a url with POST data. Some one suggest me some syntax.
2 ways to do this 1. Use PHP cURL (Example) for posting values to an URL directly from your PHP file (aka server). 2 drawbacks - cURL is not for beginners. Learn how to use it; if cURL request is made in bulk your server's IP may be penalized for spamming. 2. Use Simple HTML Javascript based POST which is used on number of sites. Write your PHP code in such a way that it generates output like following: <form id="myform" action="url_here" method="post"> <input type="hidden" name="field_1_name" value="php_generated_value" /> :::: :::: :::: </form> <script type="text/javascript"> document.getElementById('myform').submit(); </script> HTML: In this way, generate an HTML output from the PHP file, when the HTML loads, the form gets autosubmitted from the browser. 2 drawbacks - It generates OUTPUT, the post request is sent from client's browser. Unwanted access/manipulation of the submitting data is possible; the POST call doesn't work if javascript is disabled on user's browser.
You can use following codes. Make sure there is no output before calling redirect function. <?php function redirect($url) { $html = "<html><body><form id='form' action='$url' method='post'>"; foreach ($_POST as $key => $value) { $html .= "<input type='hidden' name='$key' value='$value'>"; } $html .= "</form><script>document.getElementById('form').submit();</script>"; $html .= "</body></html>"; print($html); } ?> Code (markup):
You also have the option of storing the data in cookies and retrieving that data on another page,deleting the cookie after. I wrote a pagination script that does this.