user541597
user541597

Reputation: 4345

php sessions not being detected

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

Upvotes: 1

Views: 974

Answers (4)

LHolleman
LHolleman

Reputation: 2596

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();

Upvotes: -1

Dale
Dale

Reputation: 10469

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.

Upvotes: 0

ceejayoz
ceejayoz

Reputation: 180055

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.

Upvotes: 3

ThiefMaster
ThiefMaster

Reputation: 318558

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.

Upvotes: 1

Related Questions