PHP: passing hidden variables server side

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • teren
    New Member
    • Jun 2006
    • 2

    PHP: passing hidden variables server side

    Hi all,

    I would like to send a user who lands on page.php to page2.php with some hidden variables (similar to post method).

    I assume the header redirect will only pass visible variables (similar to get method).
    e.g.
    header("locatio n: http://url.com/page2.php?var1& var2");

    Is there a way to automate the redirect and pass along variables picked up from page.php to page2.php that are not visible in the redirected url?

    is there a different method or PHP function that can accomplish this? Maybe i can create a form wiht post method and have it submit automatically on the server side?

    For various reasons, I can't do this on apache, i need to do this in the PHP code.

    thanks in advance,
    teren
    Last edited by teren; Jun 26 '06, 01:16 PM.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    There are a number of things that you can do, for instance you could

    1. Use a Session

    Use a session to store the variables you need to pass, in some cases this will add the session id to the url but this is automatic. You have to start the session before and headers are set to the client, i.e. it must be the first thing you do in the script.

    2. Use a cookie

    Use a cookie to store the variables you want to pass along. Again you must start the cookie before you send and html headers. Of course since the cookie is stored locally an inteligent user may be able to get the data out of it.

    3. Just include the page into the current script

    If it's a case of the user requests page1.php but you want to return page2.php then you could just include page2.php into page1.php at the relevent point, or if this is not possible create a fragment that can be included into page1.php and page2.php.

    4. Use hidden form inputs

    I don't think this is a particularly good idea but you could put the data into hidden form input elements and then submit the form using the POST method.



    I would probably go with either 1 or 3 personally.

    Comment

    • teren
      New Member
      • Jun 2006
      • 2

      #3
      thank you for your reply. i didn't explain myself well enough the first time. I am familiar with all those techniques. i don't think those apply here. let me try to explain the need better:

      user lands on page.php?id=123 from another domain where i can't control sessions or cookies.

      page.php takes the user and validates some information with a MySQL DB, and then sends the user on to page2.php

      page2 may be on another domain or perhaps on the same one as page.php, depending on the results of the process on this page. We need to redirect the user programatically on the server directly to page2.php without the user having to hit a submit button, but we do need to also hide the variable being sent. so i can send them to the next page with header(), but then i can't add the variable info hidden. is there another way to do this?

      for lack of better description, i am saying it's like having a post method form that is automatically submitted without the user having to click on the submit button. the #4 option above, can that be done on the server with hidden variables?

      thanks again in advance,
      teren

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        you could use sessions like he said or you can do it a more difficult way which i personally wouldn't do but you can... use hidden text fields and use a javascript to submit this form on load.

        Comment

        • iam_clint
          Recognized Expert Top Contributor
          • Jul 2006
          • 1207

          #5
          An example for automaticly posting a form
          Code:
          <script>
          function auto() {
            document.myform.submit();
          }
          </script>
          <body onload="auto();">
          <form action="page2.php" method="POST" name="myform">
          <input type="hidden" value="somevalue" name="you know">
          <input type="hidden" value="somevalue" name="what todo here">
          <input type="hidden" value="somevalue" name="so do it">
          <input type="hidden" value="somevalue" name="ok">
          </form>

          Comment

          Working...