Session values aren't being saved for later use

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
upejo
Forum Newbie
Posts: 3
Joined: Fri Jul 13, 2012 11:43 am

Session values aren't being saved for later use

Post 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>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post by social_experiment »

where do you initially set the session variables?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
upejo
Forum Newbie
Posts: 3
Joined: Fri Jul 13, 2012 11:43 am

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

Post by upejo »

$_SESSION[0] = $_GET; doesn't do it?
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

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

Post 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.
upejo
Forum Newbie
Posts: 3
Joined: Fri Jul 13, 2012 11:43 am

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

Post by upejo »

Thanks, McInfo. Got the job done.
Post Reply