PHP passing variables between pages using sessions

edhasted
edhasted used Ask the Experts™
on
I know I'm being thick but passing variables between pages in PHP seems awkward, or I'm sure I make it more complex that it needs be...

I have some variables I want to pass to another page, say a small array as a single variable.

$Car[1]="Ford";
$Car[2]="General Motors";
$Car[3]="VW";
$CarColor="Blue";

To begin the process I issue a session_start(): command.
What I don't understand is how I tell the script to stop remembering the variables I wish to add to the session and then regurgitate them on the next page?





Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Page 1:

<?php

session_start();
$Car[1]="Ford";
$Car[2]="General Motors";
$Car[3]="VW";
$CarColor="Blue";


$_SESSION['car1'] = $Car[1];
$_SESSION['car2'] = $Car[2];
$_SESSION['car3'] = $Car[3];
$_SESSION['carColor'] = $CarColor;
?>

Open in new window


Page 2:

<?php
session_start();

echo $_SESSION['car1'];
echo $_SESSION['car2'];
echo $_SESSION['car3'];
echo $_SESSION['carColor'];
session_destroy();

?>

Open in new window


hope that help

Author

Commented:
Brilliant - that's it. I did try something similar but I clearly mucked up my syntax.

Author

Commented:
Many thanks for such as fast and easy to understand solution.

I had trawled several pages on the net and they were nowhere near as understandable or concise.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial