Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Forwarding POST data to another php script

Hi,

I have an image gallery, and since it has user submitted content, I wanted my users to be shown a warning about that, and if they agree, forward them on. Something like this:

my_gallery.php
<?php
    // checking mysql table
    if (userHasNotAgreedToEULA) {
        // If they hit yes, forward them to acceptance script along with all POST data.
        echo "There might be bad stuff here, do you want to continue?:";
        echo "<a href = 'https://mysite.com/agree_to_eula.php'>Sure</a>;
        return;
    }
 
    // They already agreed, show them pictures.
    echo "<img src = 'happy_face.jpg";
}

// agree_to_eula
<?php
   
    // I need the POST data from the previous script here.
    mysql_query("update users set agreed = 1 where user_id = '" . $userid . "'");

    // Now forward them back to the original page, along with all the POST data.
    redirectTo("my_gallery.php");
?>

So I'm not sure how to "forward" the POST data to another one of my scripts, and I also don't know how to do an automatic redirect to another page (like at the bottom of the second script example). The POST data has a lot of info in it.

Thanks
Avatar of Robin Uijt
Robin Uijt
Flag of Netherlands image

Have a look at http://mrarrowhead.com/php_tutorials_mah/php_passing_variables.php

Probably the best way is to store the POST data in your session variable $_SESSION.

Redirecting to another page in php is like:

header("Location: otherpage.php");
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ray, That's a cool solution. I was thinking to repost the data with CURL
@Michael701: Thanks.  It will work with anything but the largest data sets.  However I prefer to just put the data into the data base and let the next page get the data out of the data base.  Passing it along in $_SESSION risks the situation that a client will be halfway through his work and the doorbell will ring... And while he is away, the session will time out.  

best to all, ~Ray
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

Ok I need to read up on the session stuff.

Here's my most pressing problem with this right now - at the moment, I have users going to a gateway page, and the client application sends in the POST data their unique user ID, and some other small info. This is working fine.

The release date for the client software is close, and I won't have time to build out all the web side of this project before the release.

As long as that initial page is getting all that POST data, I should in theory be able to pass it between as many pages as I need to right?

I will probably want (from the gateway page) to send users to a page where they'd enter credit card information via paypal, and stuff like that. So the POST data should be able to survive through all those page transitions?

Anything I should know about now, any limitations? My ultimate goal would be to release the client software as-is, and be able to build out the web side without any client upgrades necessary,

Thanks
Your safest approach is to put the data into the data base.  

Put a page-initialization script at the top of every page (In my scripts, my first statement is "require_once('config.php');" ) and have it retrieve the data from the data base.  Carry the client-id in the session array.  That way if you lose the session array, you just have to ask for another login.  If you have all the POST data in session, and you lose the session, well, ... Bummer!
Ok, sorry for my ignorance, last question -

So this page has to take POST data from the user, but how do I reconcile the fact that it might also be taking data from a previous session? For example, my page is just an image gallery, and I need to re-show the page, just with a different set of images - so does it take the data out of $_POST or does it take it from the $_SESSION? Do I just check to see which exists?:



// gallery.php
<?php
   session_start();
   
   $userid = "";
   $username = "";
   if (isset($_SESSION["post"])) {
       $foreign_POST = unserialize($_SESSION["post"]);
       $userid   = $foreign_POST["userid"];
       $username = $foreign_POST["username"];
   }
   else {
       // Normal POST processing routine.
       $userid   = $_POST["userid"];
       $username = $_POST["username"];
   }
   
   // Now I can do the rest of the script stuff, whether
   // the data came from the user for the first time, or
   // if this is just a 'reload'.
   <a href="gallery.php">Next Images</a>
?>

Open in new window

I think that is a business logic question I can't answer.  A general strategy might be to take everything from $_SESSION and then overwrite with whatever was in $_POST.  But it would be up to you as the app designer to choose the order of precedence.

Best, ~Ray
Ok I think that's what I want to do:

if ($_SESSION has serialized post data already) {
    use it.
}
else {
    it should be in $_POST, otherwise fail loading the page
}

And this is what I cobbled together, it seems to work. I'm not sure if I'm using PHP correctly to alias the POSTDATA variable (I just want it to be like a pointer in C/C++, not do a whole copy):
<?php 
    start_session();
 
    $POSTDATA = ""; // points to the right array to use.
    if (isset($_SESSION["post"])) {
        // Use data serialized from a previous load.
        $POSTDATA = unserialize($_SESSION['post']);
    }
    else {
        // Use data from a first-time post.
        $_SESSION['post'] = serialize($_POST);
        $POSTDATA = $_POST;
    }
 
    // Normal page processing....
    $user_name  = $POSTDATA['user_name'];
    $user_color = $POSTDATA['user_color'];
    // etc...
    
?>

Open in new window

Yes, that looks OK.  One minor change at line 2
<?php 
    session_start();
 
    $POSTDATA = ""; // points to the right array to use.
    if (isset($_SESSION["post"])) {
        // Use data serialized from a previous load.
        $POSTDATA = unserialize($_SESSION['post']);
    }
    else {
        // Use data from a first-time post.
        $_SESSION['post'] = serialize($_POST);
        $POSTDATA = $_POST;
    }
 
    // Normal page processing....
    $user_name  = $POSTDATA['user_name'];
    $user_color = $POSTDATA['user_color'];
    // etc...
    
?>

Open in new window

Thanks a lot it's working well. Got some new questions in a new Q.
Ok, please accept an answer here, and if you'd like, post links to your new questions.