1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to redirect to another url with post data in PHP

Discussion in 'PHP' started by mpchekuri, Jul 21, 2011.

  1. #1
    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.
     
    mpchekuri, Jul 21, 2011 IP
  2. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #2
    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.
     
    techbongo, Jul 21, 2011 IP
  3. dthoai

    dthoai Member

    Messages:
    106
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    38
    #3
    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):
     
    dthoai, Jul 22, 2011 IP
  4. Andre91

    Andre91 Peon

    Messages:
    197
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #4
    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.
     
    Andre91, Jul 26, 2011 IP
  5. Torafox

    Torafox Peon

    Messages:
    220
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    cURL is the most straightforward way to do this as correctly described above.
     
    Torafox, Jul 28, 2011 IP