Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts

    Supplying a page with $_POST data follwing a server-side redirect

    Hi all,

    The following example will make the variables x and y available in mypage.php as $_GET data:

    PHP Code:
    header ("Location: mypage.php?x=1&y=2"); 
    Is there a way to send variables to mypage.php using as POST data? (so that they need not be passed as part of the URL).

    EDIT: I know I can mimick this using $_SESSION, but I wonder if there is a neat way of incorporating it into a request before the redirect (which ellicits a response) so that I don't have to do it explicitly with $_SESSION.
    Last edited by mlse; 08-13-2008 at 11:31 AM.

  • #2
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    hidden form and javascript submit instead of a php based header redirect
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #3
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Yep, thanks, I know that will work, but I would like to do it purely server-side.

  • #4
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    I cannot think of a way to do this with just the header() function but you could use cURL to send the POST data.

    Then you don't even have to leave the page, i don't know if this is exactly what you were looking for but...

    POST Example:
    PHP Code:
    <?php
    $ch1 
    curl_init();

    curl_setopt($ch1CURLOPT_URL,"http://www.example.com/page.php");
    curl_setopt($ch1CURLOPT_POST1);
    curl_setopt($ch1CURLOPT_POSTFIELDS,"x=hello&y=goodbye");

    curl_exec ($ch1);
    curl_close ($ch1); 
    ?>
    GET Example:
    PHP Code:
    <?php
    $ch 
    curl_init();

    curl_setopt ($chCURLOPT_URL"http://www.example.com/page.php?x=hello&y=goodbye");
    curl_setopt ($chCURLOPT_HEADER0);

    curl_exec ($ch);

    curl_close ($ch);
    ?>

  • #5
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Hi, yep I had thought of that, but I think the $_SESSION solution is easier. I'll stick with $_SESSION until someone comes up with something else!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •