Link to home
Start Free TrialLog in
Avatar of Donnie Walker
Donnie WalkerFlag for United States of America

asked on

How do I pass and get a session in PHP?

I am trying to create a session on one page and then get them on another...but I'm getting this error:

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at public_html/order/thanks.php:2) in public_html/order/thanks.php on line 3

here is the code for creating the sessions:

$email = $_POST["element_6"];
$dollar = $_POST["element_13_1"];
$cents = $_POST["element_13_2"];
$item = $_POST["element_7"];

session_start();

$_SESSION["email"]=$email;
$_SESSION["dollar"]=$dollar;
$_SESSION["cents"]=$cents;
$_SESSION["item"]=$item;


here is the thanks.php page:

session_start();

$_SESSION["email"]=$email;
$_SESSION["dollar"]=$dollar;
$_SESSION["cents"]=$cents;
$_SESSION["item"]=$item;

mail("".$email."", "Order", "Thank you for your order!\n".$email."\nPaid: $".$dollar.".".$cents."\n".$item."\n");
mail("me@domain.com", "New Hosted Order", "".$email."\nPaid: $".$dollar.".".$cents."\n".$item."\n");

session_unset();
session_destroy();


I'm sure I'm doing alot wrong. For one thing the emails are empty. :)

Any help is appreciated.
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

session_start(); must be at the top of Every Single page in the session.  It doesn't just 'start' the session, it renews it and identifies the $_SESSION variables.  If you try to set $_SESSION variables before starting the session, they don't go anywhere.

Also, session_start(); like header() and cookies must be before anything on the page is sent to the browser including any white space.  A single space before the opening <?php tag will cause an error.

Sessions are not terribly difficult but they are not what most people seem to think they are.  And they are not as simple as people think they are.  Their primary purpose is to identify $_SESSION data.  http://php.net/manual/en/book.session.php
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Donnie Walker

ASKER

Thanks Dave!
You're welcome, glad to help.