Solved

PHP passing variables between pages using sessions

Posted on 2011-09-02
3
209 Views
Last Modified: 2012-05-12
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?





0
Comment
Question by:edhasted
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
boon86 earned 500 total points
ID: 36475074
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
0
 

Author Comment

by:edhasted
ID: 36475168
Brilliant - that's it. I did try something similar but I clearly mucked up my syntax.
0
 

Author Closing Comment

by:edhasted
ID: 36475182
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.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question