2

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?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

5 Comments

using session_start gives me this warning: Warning: session_start(): Cannot send session cache limiter - headers already sent
@James: Track the cause of this error by inspecting the code at the mentioned position. Or call ob_start before outputting anything else.
Are you putting session_start before the ob_start?
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: Are you putting session_start before ob_start? Could you please post complete code samples on pastebin or something?
1

session_start() in 2 files before any output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.