Skip to main content
Inspiring
May 9, 2012
Question

Question about Session variables

  • May 9, 2012
  • 1 reply
  • 876 views

Just looking for some advice to see if I'm doing something remotely correctly with Sessions.... (not something I use too often)

I basically have a simple enough database structure with two tables:

Lodges - LodgeID, Lodge, etc

Nominations - NominationID, Nomination, LodgeID, etc

A one-to-many relationship, with each Lodge being able to have multiple Nominations.

So I have a Lodge Details page, which shows the details of a selected Lodge, and a little table listing the Nominations its had.

On this page I have a link to add a new Nomination, which should pick up the LodgeID of the current Lodge, so that the new Nomination is tied to the current Lodge.

So on my Lodge details page I have a session created using:

<?php

if (!session_id()) session_start();

if (!isset($_SESSION["Lodge_ID_Number"]))     {

  $_SESSION["Lodge_ID_Number"] = "".$row_WADAlodges['LodgeID']  ."";

}

?>

On my Add Nomination page I have a hidden field that calls up the $_SESSION["Lodge_ID_Number"]:

<input type="hidden" name="LodgeID" id="LodgeID" value="<?php echo $_SESSION['Lodge_ID_Number']; ?>" />

And on my Nomination Added page I use unset to clear that Session using:

<?php

unset($_SESSION['Lodge_ID_Number']);

?>

Hope that all makes sense.... it does seem to work, but I'm just interested to know if that's the best way of doing this, or if there's a better way?

The thing that's bugging me is what would happen if the user got as far as the Add Nomination page, and then hit the back button, went to a different Lodge, before the Session variable is cleared which might cause problems?

I'd post a link, but its in a password protected admin section of a site.

This topic has been closed for replies.

1 reply

Inspiring
May 10, 2012

I've changed this now to use the URL variable. I am used to using that to go from a results page to a details page where the recordset on the details page is used. I hadn't realised that you could just set a link to:

<a href="addNomination.php?LodgeID=<?php echo($_GET['LodgeID']); ?>">Add Nomination</a>

where there is no recordset on a page, just for the purposes of passing the variable. (Yeah, I know - this is probably really basic!)

I do have one last question on this though - my form action is:

<form method="post" id="form1" action="<?php echo KT_escapeAttribute(KT_getFullUri()); ?>">

And then the link to a NominationAdded page is in the php code at the top of the AddNomination page:

$ins_nominations->registerTrigger("END", "Trigger_Default_Redirect", 99, "NominationAdded.php");

What would be useful would be to pass the URL variable through again, so that on the NominationAdded page I can have a link back to the same Lodge to add another Nomination without finding the Lodge again.

But I'm not sure what the syntax would be, as it included some PHP within existing PHP, so not just:

$ins_nominations->registerTrigger("END", "Trigger_Default_Redirect", 99, "nominationAdded.php?LodgeID=echo($_GET['LodgeID'])");

I assume this must be possible, but not sure of the exact syntax?

Participating Frequently
May 10, 2012

I don't do PHP, but I think you'd just need:

$ins_nominations->registerTrigger("END", "Trigger_Default_Redirect", 99, "nominationAdded.php?LodgeID=$_GET['LodgeID'] ");

If that doesn't work, then you may have to concatenate:

$ins_nominations->registerTrigger("END", "Trigger_Default_Redirect", 99, "nominationAdded.php?LodgeID=" . $_GET['LodgeID']);