Sessions and using Session Variables across multiple pages

I need some help with session variables. I have multiple independent bits of php code which all do what they are supposed to do, individually, but I can’t seem to get sessions working at all consistently. Sometimes it works, sometimes it doesn’t even if I think I’ve declared the variables in the same way. Anyways, here’s the simplest example of what I’m trying to do…

Intro Page: Let’s the user input their firstname, surname and email.
Programme Page then welcomes them by basically saying “Hi $firstname $surname, your email address is $email.”

Below is the code… I’m sure it is something very obvious and simple ( and systematic ). I am trying to learn php myself so have no lecturer to go to for a hand.


<?php session_start(); $_SESSION['firstname'] = $firstname; $_SESSION['surname'] = $surname; $email = $_SESSION['email']; $vignette = $_SESSION['vignette']; $debugvignette = $_SESSION['debugvignette']; ?>

// various html bits creating the sign-in template in html

<?php $firstname = $_POST['firstname']; $surname = $_POST['surname']; $email = $_POST['email']; ?>


… I initially had the sessions declared after the firstname, surname and email variables were created but that didn’t work either.



<?php session_start() ?>

// html stuff

<?php session_start(); print "Welcome to the Automated Vignette Generator " . $_SESSION['firstname'] . " " . $_SESSION['surname'] . ". Please feel free to try the vignette below. Your email is " . $_SESSION['email'] . "."; ?>


I think it must have something to do with where I declare the session variables in the INTRO page and my use of multiple session_start()s in the PROGRAMME page but I’m stumped. If someone could show me how to do it once I’m sure I can extrapolate the solutions for the rest.

Many thanks for any advice…
Apologies if this doesn’t fit the posting guidelines. I read it and tried my best, I’d be happy to modify it if needed.

Sponsor our Newsletter | Privacy Policy | Terms of Service