Solved

PHP passing variables between pages using sessions

Posted on 2011-09-02
3
205 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now