Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

Modifying PHP session variables

Hello all,


I am having a little trouble modifying PHP session variables and was hoping the community could shed some light. My objective is to have a 3 part form with several variables registered is a session per page. Registering the sessions works fine, however if the user hits the back button they are unable to modify the value of the variable. Here is the code snippet:

The form:
<?php
Header('Cache-Control: no-cache');
Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
Header('Pragma: no-cache');

     session_start();
     
     if (!$PHPSESSID)
     {
          session_register('var');
          session_register('var2');
     }
?>

<form method="POST" action="test2.php?<?php echo ("$PHPSESSID"); ?>">
  <p>
  VAR 1&nbsp;&nbsp;&nbsp;
  <select size="1" name="var">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  </select></p>
  <p>VAR 2&nbsp;&nbsp;&nbsp;
  <select size="1" name="var2">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  </select></p>
  <p>&nbsp;</p>
  <p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>



And the "POSTED" to page:

<?
Header('Cache-Control: no-cache');
Header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
Header('Pragma: no-cache');
     
     session_start();

     if (!$PHPSESSID)
     {
          session_register('var');
          session_register('var2');
     }


     echo ("varone=$var<br>");
     echo ("vartwo=$var2<br>");
?>



Any help would be appreciated.


Thanks
0
ipc
Asked:
ipc
1 Solution
 
Richard QuadlingSenior Software DeverloperCommented:
Something that MAY be useful is to add a timestamp as a query on the action ...

<script language="JavaScript"><!--
document.writeln('<form method="POST" action="test2.php?<?php echo ("$PHPSESSID"); ?>&ts=' + date.UTC() .'">');
//--></script>



I think that is right!

Basically you are using the client to add another unique parameters which, in most cases, will cause the URL to be different which should force a resend the request properly, rather than relying on some cached value.

Richard.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now