?
Solved

PHP passing variables between pages using sessions

Posted on 2011-09-02
3
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
boon86 earned 2000 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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