ematthys Posted September 2, 2007 Share Posted September 2, 2007 I put a simple little script on my site to test my sessions that looked like this: Page 1: <?php // page1.php session_start(); $_SESSION["real_name"] = "Test Name"; print "<a href="page2.php">Go to this page</a>"; ?> Page 2: <?php // page2.php session_start(); print $_SESSION["real_name"]; ?> This worked fine. Now I am trying to use sessions in a fairly simple example to save if someone has voted on a particular survey. I can print out the session id and it is consistent each time, but the $_SESSION variable I am trying to set disappears once you change pages, unlike in my example. Can someone please tell me what I am doing wrong? This is the part of the code that uses sessions. I do not get any errors, the $_SESSION variables are just disappearing the second you leave the page. I do not understand why this is happening here, but not in my example. Thanks in advance. <?php if ( !isset($TEMPLATE) ) { session_start(); $TITLE = "Surveys"; include ($_SERVER['DOCUMENT_ROOT'] . "/template/template.inc.php"); } ?> <?php if (isset($_POST['optionscount'])) { $optionscount = $_POST['optionscount']; $survey_id = intval($_GET['survey']); if (isset($_SESSION["survey".$survey_id])) { echo("<br /><div class=\"error\">You cannot vote more than once.</div>"); } else { if (isset($_POST['options'])) { $option = $_POST['options']; $rs = mysql_query("SELECT ".$option." FROM surveys WHERE id=".$survey_id, $db); $row = mysql_fetch_assoc($rs); if ($row) { $current_count = ($row[$option] + 1); $submit_query = "UPDATE surveys SET ".$option."=".$current_count." WHERE id=".$survey_id; $submit_rs = mysql_query($submit_query, $db); if ($submit_rs) { $message = "Thank you for your vote!."; $class = "success"; } else { $message = "Error: " . mysql_error(); $class = "error"; } $_SESSION["survey".$survey_id] = "voted"; } else { echo("<br /><div class=\"error\">There was an error with the option you selected.</div>"); } } else { echo("<br /><div class=\"error\">You must select an option.</div>"); } } } ?> Quote Link to comment Share on other sites More sharing options...
php_tom Posted September 2, 2007 Share Posted September 2, 2007 Can you explain what this line does? if ( !isset($TEMPLATE) ) { I'm thinking that if "isset($TEMPLATE)" is always true, you would never call session_start(), so when you set the vars in $_SESSION they wouldn't pass through to the next page (if that made any sense). But maybe I'm wrong. Quote Link to comment Share on other sites More sharing options...
ematthys Posted September 2, 2007 Author Share Posted September 2, 2007 $Template is a variable that is set by the template.inc.php file that basically draws the template of the page around the content that goes in the actual file. It shouldn't be set until after the template.inc.php file is called. Quote Link to comment Share on other sites More sharing options...
ematthys Posted September 2, 2007 Author Share Posted September 2, 2007 I also tried with the auto_start feature on, but that didn't help anything either. Quote Link to comment Share on other sites More sharing options...
php_tom Posted September 2, 2007 Share Posted September 2, 2007 Try var_dump($_SESSION) both in the page where you set the session vars, and in the next page where you try to access them again. Post the output. Quote Link to comment Share on other sites More sharing options...
Crew-Portal Posted September 2, 2007 Share Posted September 2, 2007 Try making a single php file that has all of your session superglobals and session_register built into it! Then simply use PHP include(); in every document (Even the ones that everone can access) this way if you have an error in your document the session wont drop but will be carried on into the next doument Quote Link to comment Share on other sites More sharing options...
ematthys Posted September 3, 2007 Author Share Posted September 3, 2007 Well this is very odd. I was working on other parts of the page today and came back to read the posts. I tried the var_dump to see what it would say and what do you know? It works! I must have done something to another part of the template that fixed it somehow. Very strange indeed...haha Thanks for all your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.