Session variable - Basic question

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
jarow
Forum Commoner
Posts: 83
Joined: Tue Jan 28, 2003 2:58 am

Session variable - Basic question

Post by jarow »

I am very new to PHP, session variables and arrays so bear with me.

I have a 2 page form with several form fields on each page. I need to store the data from page 1 form fields, bring it along to page 2 and then submit all the data into my database.

My question is....is there a way of creating an array that would store all my data from page 1 or do I have to list each field separately? Would I need to create hidden fields on page two for the fields I have stored from page 1?

If you could please write out a basic example I would be most appreciative....and assume I know nothing!

<?php

session_start();

????

?>

Many thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

this example by far does not cover all aspects of sessions but it might be an entry-point ;)
(your php version is 4.3.0, right? comments refer to default-settings)

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);  // debug-settings
/**
	php sessions use cookies by default and cookies are sent within the response header
	so this have to take place before any content (the response body, in this case an html-document) is written
*/	
session_start();
/**
	php has determined the session-id from the data sent with this request
	and (if present) loaded the appropriate dataset from a flat file to the superglobal array $_SESSION
		or created a new session-id
*/	
?>
<html>
	<body>
<?php

if(isset($_POST['mode']) && $_POST['mode']=='clear session')
{
	// erase all elements of $_SESSION by assigning a new, emtpy array
	$_SESSION = array();
	echo '$_SESSION set empty';
}
elseif(isset($_POST['mode']) && $_POST['mode']=='destroy session')
{
/**
	this does not! advise the client not to use the session-id anymore
	but it turns off php's session handling, e.g. no serialization of session data will take place
*/	
	session_destroy();
	echo 'session destroyed';
}
else
{
?>
		your session id is: <?php echo session_id(); ?>
		<fieldset>
			old session values:
			<pre><?php print_r($_SESSION); ?></pre>
<?php
/**
	 adding all parameters that have been POSTed with this request
	 by merging the arrays $_SESSION and $_POST
	 elements in $_POST having the same name as in $_SESSION will overwrite 'old' elements
*/
$_SESSION = array_merge($_SESSION, $_POST);

//	and a simple counter
$_SESSION['counter'] = isset($_SESSION['counter']) ? $_SESSION['counter']+1 : 0;
?>
			new session values:
			<pre><?php print_r($_SESSION); ?></pre>
		</fieldset>
<?php
} // end-of $_POST['mode']-cases
?>
		
		<form	method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
			a field that always has the same name: <input type="text" name="static" /><br />
			a field with a random name <input type="text" name="random<?php echo rand(1,1000); ?>" /><br />
			you will figure out the name of this field  <input type="text" name="field<?php echo count($_SESSION); ?>" /><br />
			<input type="submit" name="mode" value="add" /><br />
			<input type="submit" name="mode" value="clear session" /><br />
			<input type="submit" name="mode" value="destroy session" />
		</form>
	</body>
</html>
<?php
/**
	at the end of the script php will serialize $_SESSION if there's still a session active
*/
?>
before trying it run

Code: Select all

<?php phpinfo(); ?>
check wether session.save_path is set to a valid path.
Last edited by volka on Mon Mar 31, 2003 6:53 am, edited 1 time in total.
User avatar
d1223m
Forum Commoner
Posts: 80
Joined: Mon Mar 31, 2003 5:15 am
Location: UK, West Sussex

Post by d1223m »

the simplest way is to use hidden fields in your pages but sessions work too.

for sessions look at session_register iirc. you simply pass to it the name of the variable you want to session'ize.

with hidden fields you simply do something like:

Code: Select all

---form1.php--

<form action="form2.php" method="post">
<input type="text" name="fred">
<input type="submit" >
</form>

--form2.php--

<form action="final.php" method="post">
<input type="hidden" name="fred" value="<? print $_POST&#1111;'fred']; ?>">
<input type="text" name="bob">
<input type="submit">
</form>

--final.php--

print $_POST&#1111;'fred'];
print $_POST&#1111;'bob'];
Post Reply