0

Possible Duplicate:
Headers already sent by PHP

I'm facing a problem while trying to pass variables from one page to another (php) I have page 1 which automatically load page 2 when you access it (using jquery). In order to page 2 work properly I need to pass 2 variables so here is my way to do so:

on Page 1:
session_start();
$_SESSION['username'] = $username;
$_SESSION['user_id'] = $user_id;

on Page 2:
session_start();
$username = $_SESSION['username'];
$user_id = $_SESSION['user_id'] ;

On the first load, it doesn't work. If I refresh, everything is fine. If I go to another computer or clear the cache of the current browser, I go through the same problem, On the first load, it doesn't work. after refreshing, everything is fine.

error log message:

[01-Oct-2012 20:02:59] PHP Warning: session_start() [<a href='function.session-start'>function.session-start</a>]: Cannot send session cookie - headers already sent by (output started at /public_html/php/info.php:1) in /public_html/php/info.php on line 26

does anyone have an idea what is the cause of this issue?

Community
  • 1
  • 1

2 Answers2

0

The classic headers already sent error* (as seen in a comment). The issue is that you're calling session_start() after you've already outputted something, either through echo or regular HTML (among other possible output methods).

Move your session_start() call to the very top of your script in both/all pages and that should resolve your issue.

* The headers already sent error is, as I've said, because you're outputting data to the page before you send a header. In web applications, you have to send all of the headers first (including the session headers) before you output any data/content.

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
0

Cookies are needed for session handling (some exceptions, depending on your server configuration). And cookies are send in the header, before any other information is sent.

The error message you see is telling you that some content is sent before you execute session_start();

One of the usual suspects is having blank line(s) before your opening PHP tags (<?php) at line one.

See this question for more details.

Community
  • 1
  • 1
Roflo
  • 235
  • 1
  • 9
  • 27