James
James

Reputation: 111

PHP Session over 2 pages

i have the following code:

if (!isset($_SESSION)) {
ob_start();
}
$_SESSION['views'] = 1; // store session data
echo $_SESSION['views']; //retrieve data

i tried to break it up in 2 parts like this:

//Page 1
if (!isset($_SESSION)) {
ob_start();
}
$_SESSION['views'] = 1; // store session data

.

//page 2
echo $_SESSION['views']; //retrieve data

it echos nothing, what am I doing wrong?

Upvotes: 2

Views: 1314

Answers (3)

easyjo
easyjo

Reputation: 734

as Gumbo mentioned, you need to call session_start() on every page you want to use the session..

You also mentioned you were getting the error: Warning: session_start(): Cannot send session cache limiter - headers already sent

This is due to data being output on the page before the session_start() is being called, you have to include this before anything is echo'd to the browser.

Upvotes: 2

Alex Pliutau
Alex Pliutau

Reputation: 21957

session_start() in 2 files before any output.

Upvotes: 1

Gumbo
Gumbo

Reputation: 655269

Make sure to call session_start on every page you want the session to be available. ob_start is not the session handler but the output buffer handler.

Upvotes: 1

Related Questions