1

I have a login page that I register two sessions username and password. then redirect to another page. Once at this page

$_SESSION['username'] = "";
$_SESSION['password'] = "";

after login check I have the next page check if the session is registered

session_start();

if(isset($_SESSION["username"]){


continue

}else go back to login page

Once I'm logged in I want to go to another page that depending on if the session variable is set I display something different on the page.

So on the galery page I do

at the very top of page I do

<?php 
session_start();

?>

then further down where I want the button to be I do

<?php
if (isset($_SESSION['username'])){


show a new button

}

?>

I get the button to show but at the top of the page I have

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

and it messes up how my page is displayed. Any ideas? I have the session_start(); at the very begging of page I don't know why this is happening. Any ideas?

where my session_start() is located

user541597
  • 4,247
  • 11
  • 59
  • 87
  • possible duplicate of [Headers already sent by PHP](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) (The reference question / answer) – hakre Apr 12 '12 at 18:39
  • 1
    The rest of the error message (that part you did not post) tells you in which line the output started. Use the information the error message gives you to solve your problem. – hakre Apr 12 '12 at 18:40
  • Did you do a session_start() before assigning those two session variables? – Marc B Apr 12 '12 at 18:42
  • yes I did a session_start() before assigning those two variables. – user541597 Apr 12 '12 at 18:44

4 Answers4

3

You'll get that error if anything outputs to the browser before you call session_start(). For example, you can't do:

<?php

echo "Test";

session_start();

You also can't do:

 <?php session_start();

(note the space before the <?php)

Make sure nothing - no HTML, no blank lines, no spaces - is written out prior to your session_start() calls and you'll be fine.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • theres nothing written. – user541597 Apr 12 '12 at 18:41
  • Is that file being included by something else? This is a very well known error, and in every single time I've ever seen it asked the eventual response is "oops, found where the whitespace was coming from". – ceejayoz Apr 12 '12 at 18:42
1

You need to ensure there's nothing (whitespace, UTF-8 BOM) before your <?php. This also applies to any files you include before the session_start() call.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

Is the gallery page being included in another file?

<?php

// lots of php code

include('/path/to/gallery.php');

?>

If anything is being sent to the browser before the session_start(); it will create this error.

Dale
  • 10,384
  • 21
  • 34
-1

Session is a header, headers can't be sent after any output (even a single space). You got 2 options, place session_start() before ANY output or you could also use output buffering, this allows you to send headers after output.

Place this at the top of your script

ob_start();

And this at the end

echo ob_get_clean();
LHolleman
  • 2,486
  • 2
  • 18
  • 19