tags:

views:

39

answers:

3

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?

+1  A: 

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.

Gumbo
using session_start gives me this warning: Warning: session_start(): Cannot send session cache limiter - headers already sent
James
@James: Track the cause of this error by inspecting the code at the mentioned position. Or call `ob_start` before outputting anything else.
Gumbo
Are you putting session_start before the ob_start?
malonso
i tried doing both pages with ob_start and nothing was output, then i tried 1 with session and 1 with ob - same thing. i then tried the with ob and session on the same page but i stil get the error
James
@James: Are you putting session_start before ob_start? Could you please post complete code samples on pastebin or something?
malonso
+1  A: 

session_start() in 2 files before any output.

Alexander.Plutov
+1  A: 

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.

easyjo
this helped me fix it, just needed to put the session start right at the top of the page
James