Jump to content

[SOLVED] PHP session info disappearing


ematthys

Recommended Posts

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>");
		}
	}
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.