how do I add a new session variable

Discussion in 'PHP' started by jalea148, Jul 25, 2007.

  1. #1
    I have multiple pages, each with a form and all in the same domain. The page 1 form posts to page 2 and the session variables are created using $_POST[.... This works fine.

    New fields are added on the page 2 form. This form posts to page 3. I've been unsussessful getting this information to the 3rd page either by creating additional session variables on page2 or using $_POST[ on page 3. The 1st set of session variables are available to page 3.
     
    jalea148, Jul 25, 2007 IP
  2. ivenms

    ivenms Peon

    Messages:
    334
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
  3. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Show us some code. I don't understand what you are explaining.

    You will need to do a start_session(); on top of each page. and then use $_SESSION['variable'] and each page. That is a way the variables can be passed to each page.
     
    exodus, Jul 25, 2007 IP
  4. jalea148

    jalea148 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Page 1 form extract:

    <td valign="top"><form id="form_weber" name="form_weber" method="post" action="http://www.source.com/temp/Quote_FORM.php" >

    <table width="346" border="2">
    <tr>
    <td width="150"><div align="left">First Name </div></td>
    <td><label>
    <input name="First" type="text" id="First" />

    Page 2 extracts:

    <?php
    session_start();
    session_destroy();
    session_register("sessFirst");
    session_register("sessLast");
    session_register("sessAddr");

    session_register("sessCounty");
    session_register("sessSex");

    $sessFirst = $HTTP_POST_VARS['First'];
    $sessLast = $HTTP_POST_VARS['Last'];
    $sessAddr = $HTTP_POST_VARS['Addr'];
    ?>

    …………………………
    <script language=JavaScript>
    function validateForm() {
    with (document.quote_form) {
    var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";
    // if (name.value == "") alertMsg += "\nname";
    radioOption = -1;
    for (counter=0; counter<Appl_Sex.length; counter++) {
    if (Appl_Sex[counter].checked) radioOption = counter;
    }
    if (radioOption == -1) alertMsg += "\nApplicant Sex";
    if (County.options[County.selectedIndex].value == "") alertMsg += "\nCounty";
    if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n") {
    alert(alertMsg);
    return false;
    } else {
    return true;
    }}}

    function SetSessSex(s){
    $sessSex = s;
    }
    </script>

    ……………………………………
    "http://www.source.com/tempj/weber.php" method="post" enctype="multipart/form-data" name="quote_form" id="quote_form" onsubmit="return validateForm()">

    <table border="8" cellpadding="1" id="Form0">
    <tr>
    <td colspan="2"><div align="right" class="style12">
    <p>First</p>
    </div></td>
    <td width="87"><p>
    <input name="$First" type="text" class="req" id="$First" tabindex="15" size="50" value="<?php echo $sessFirst; ?>" />
    </p></td>

    ……………………………….. [here’s the start of the problem]
    <tr>
    <td bgcolor="#E2D5FF" class="style20"><input name="Appl_Sex" id="App_Male" class="req" type="radio" value="Male" tabindex="20" onClick="setSessSex('Male')" />
    <label> Male </label>
    <input type="radio" name="Appl_Sex" id="App_Female" class="req" value="Female" tabindex="21" onClick="setSessSex('Female')" />
    <label > Female </label></td>
    </tr>

    On page 3 $sessSex is null and $_POST[‘App_Sex’] is null and $HTTP_POST_VARS['App_Sex'] is null.
    $First etc are correct on page 3.

    The following is currently at the top of page 3 [1 of several variations]:

    <?php
    session_start();
    session_register("sessSex");
    session_register("sessCounty");
    $sessSex = $HTTP_POST_VARS['Appl_Sex'];
    $sessCounty = $HTTP_POST_VARS['County'];
    ?>
     
    jalea148, Jul 25, 2007 IP
  5. jalea148

    jalea148 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    IVENMS:
    I believe I've been complying with all the requirements stated in the article. I believe they are in the code extracts I just sent. The article is good - thanx.
     
    jalea148, Jul 25, 2007 IP
  6. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Session variables are stored in the $_SESSION super global.

    if you want to create a new session variable you simply do

    $_SESSION['variable_name'] = $your_value;

    There's no need to use session_register. It's generally best to use the super globals to avoid security issues.
     
    KalvinB, Jul 25, 2007 IP
    debunked likes this.
  7. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #7
    Your problem is your destroying the sessions on each page. I think you are looking at a old tutorial on how to do session varibles to, because the session register is no longer required. You can simplify the code by using the below. session_destory will erase all the session variables and only use it after you have finished with all the session variables.

    
    <?php
    session_start();
    
    $_SESSION['sessFirst'] = $_POST['First'];
    $_SESSION['sessLast'] = $_POST['Last'];
    $_SESSION['sessAddr'] = $_POST['Addr'];
    
    ?>
    PHP:

    .
    I like my answer please, don't forget to rep.
     
    exodus, Jul 25, 2007 IP
  8. ivenms

    ivenms Peon

    Messages:
    334
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Dont try to make too much session variables.

    This can make overload to your server.
     
    ivenms, Jul 26, 2007 IP
  9. jalea148

    jalea148 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    At this point on page2:

    <?php
    session_start();
    session_register("sessFirst");
    session_register("sessLast");
    session_register("sessAddr");
    $sessFirst = $HTTP_POST_VARS['First'];
    $sessLast = $HTTP_POST_VARS['Last'];
    $sessAddr = $HTTP_POST_VARS['Addr'];
    ?>
    ………………………..

    ] <script language = "JavaScript" type="text/javascript">

    function SetSessSex(s){
    $_SESSION['$sessSex'] = s;
    }
    </script>
    …………………………………

    <tr>
    <td bgcolor="#E2D5FF" class="style20"><input name="Appl_Sex" id="App_Male" class="req" type="radio" value="Male" tabindex="20" onClick="setSessSex('Male')" />
    <label> Male </label>
    <input type="radio" name="Appl_Sex" id="App_Female" class="req" value="Female" tabindex="21" onClick="setSessSex('Female')" />
    <label > Female </label></td>
    </tr>

    and on page 3

    <?php
    session_start();
    ?>
    ……………………………


    <input type="hidden" name="First" value="<?php echo $sessFirst; ?>">
    <input type="hidden" name="Last" value="<?php echo $sessLast; ?>" >
    <input type="hidden" name="Address1" value="<?php echo $sessAddr; ?>" />
    <input type="hidden" name="Sex" value="<?php echo $sessSex; ?>" />

    First, Last and Address1 are correct.
    Sex = “”

    IMHO, I have doubts about mixing Javascript with PhP. In the routine
    function SetSessSex(s){
    $_SESSION['$sessSex'] = s;
    }

    the script is run on the Client side and the session variable wants to be set on the server. Just a WAG.
     
    jalea148, Jul 26, 2007 IP
  10. jalea148

    jalea148 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I tried to use on page 3 $_POST['Appl_Sex'] - this was null.
     
    jalea148, Jul 26, 2007 IP
  11. jalea148

    jalea148 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    A new strategy to resolve this:

    I put this on page3:

    <?php
    if (isset($HTTP_RAW_POST_DATA)) {
    $request = $HTTP_RAW_POST_DATA;
    } else {
    $request = file_get_contents('php://input');
    }
    echo "HTTP_RAW_POST_DATA=";var_dump($HTTP_RAW_POST_DATA);
    echo "<br />php://input=";var_dump(file_get_contents('php://input'));
    echo "<br />request=";var_dump($request);

    It responds:

    HTTP_RAW_POST_DATA=NULL
    php://input=string(0) ""
    request=string(0) ""

    I suspect the php.ini file may be preventing posts from multiple pages - what do you think?
     
    jalea148, Jul 26, 2007 IP
  12. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #12
    No, I think you are just doing it. Wrong. You totally ignored what I said and wrote.


    Also, SESSIONS are server side. You will not be able to access them via Javascript Code like you are doing. You can how ever do it through AJax and post/get via ajax as a user enters information. I think you don't understand though.
     
    exodus, Jul 26, 2007 IP
  13. jalea148

    jalea148 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    I'll give AJAX a shot; thanx. BTW, on the last line of my reply of 10:37 today I said session variables were server side.
     
    jalea148, Jul 26, 2007 IP
  14. jalea148

    jalea148 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    ;) Problem solved [sorta]. Couldn't use [or understand] AJAX, but found my server does not support AJAX [and doesn't want to].
    Now everything works except page 2 uses the Get Method [pagewithPOST.html ==>> pagewithGET.php ==>> pagewithPOST.php ==>> aclientservice.html]. All the data I need to pass through does. It would be much nicer to be able to use the Post method throughout, but this works.
     
    jalea148, Jul 27, 2007 IP