Page 1 of 1

Session values aren't being saved for later use

Posted: Fri Jul 13, 2012 12:59 pm
by upejo
Hi! I'm a PHP-newbie and stumbled upon a problem which is about to get me quite frustrated. What I'm attempting to write is a harmonograph capable of remembering the parameters of five previously generated figures. Drawing is done in Flash and the user interface relies on PHP. For some reason the $_SESSION appears to be empty every time I reload the page.

Code: Select all

<?php
	session_start();
?>
<html>
<head>
	<link rel="stylesheet" type="text/css" href="hGraphStyle.css" />
</head>
<div id="wrapper">
	<div id="flash">
		<?php
				$supt = $_GET['supt']; 
// ... gets eighteen different values ...
				$fps = $_GET['fps']; 
// ... echoes, unrelated to the issue ...
				for ($i = 0; $i <= 4; $i++) { // Is supposed to generate thumbnail pics of the previuos figures
					if (isset($_SESSION[$i])){ // in case there's the information to draw them from
						echo "<tn>";
						echo "<object type='application/x-shockwave-flash' data='hGraphTn.swf?supt=".$_SESSION[$i]['supt'].
						"&dt=".$_SESSION[$i]['dt']."&Ax=".$_SESSION[$i]['Ax']."&fx=".$_SESSION[$i]['fx'].
						"&dfx=".$_SESSION[$i]['dfx']."&px=".$_SESSION[$i]['px']."&dpx=".$_SESSION[$i]['dpx'].
						"&As=".$_SESSION[$i]['As']."&fs=".$_SESSION[$i]['fs']."&dfs=".$_SESSION[$i]['dfs'].
						"&ps=".$_SESSION[$i]['ps']."&dps=".$_SESSION[$i]['dps']."&Ay=".$_SESSION[$i]['Ay'].
						"&fy=".$_SESSION[$i]['fy']."&dfy=".$_SESSION[$i]['dfy']."&py=".$_SESSION[$i]['py'].
						"&dpy=".$_SESSION[$i]['dpy']."&d=".$_SESSION[$i]['d']."&fps=".$_SESSION[$i]['fps'].
						"' width='100' height='100'></object>";
						echo "</tn>";
					}
				} 
// ... echoes, unrelated to the issue ...
				for ($k = 4; $k > 0; $k--) {
					if (isset($_SESSION[$k-1])) $_SESSION[$k] = $_SESSION[$k-1];
				}
				$_SESSION[0] = $_GET; // saves the current figure
		?>
	</div>
</div>
</html>

Re: Session values aren't being saved for later use

Posted: Fri Jul 13, 2012 3:10 pm
by social_experiment
where do you initially set the session variables?

Re: Session values aren't being saved for later use

Posted: Fri Jul 13, 2012 7:34 pm
by upejo
$_SESSION[0] = $_GET; doesn't do it?

Re: Session values aren't being saved for later use

Posted: Sat Jul 14, 2012 8:43 am
by McInfo
Session keys/indexes cannot be numeric. With error_reporting and display_errors enabled you would see a notice about "skipping numeric key" which means that the item in question will not be stored in the session. Session keys should use variable-naming conventions. The simplest solution would be to prefix the numbers with an underscore. For example: $_SESSION['_0'], $_SESSION['_1'], etc.

Re: Session values aren't being saved for later use

Posted: Mon Jul 16, 2012 5:18 pm
by upejo
Thanks, McInfo. Got the job done.