-1

I really wonder that why that warning occurred. I think my code is okay, but it said that headers already sent(output started at ...../request.php:1)in ...../request.php on line 2

Here is my code

<?php
session_start();
if(!isset($_SESSION['login'])){
session_destroy();
header("Location:login.php");
exit();
}
include_once("action.php");
?>

Please help me out, Thank in advance

Sovat
  • 59
  • 1
  • 8
  • what exact error you have? It seems you send something to the browser before header() – bksi Jun 02 '13 at 03:30
  • 3
    Check for any **white space** before your ` –  Jun 02 '13 at 03:31
  • 1
    You should remove the BOM signature as well if your file has it. Older servers will fail to recognize the BOM signature and send it as output. – Fabrício Matté Jun 02 '13 at 03:34
  • @NOX, what is white space? I'm new to PHP. Thank for telling – Sovat Jun 03 '13 at 03:15
  • @FabrícioMatté, Remove BOM? I don't understand so far, can you detail me about this? – Sovat Jun 03 '13 at 03:16
  • @Sovat any **space**, **tab** or **return** before you php tag, something like this: " –  Jun 03 '13 at 03:17
  • @NOX, i have checked it is no whitespace. But that warning still occurred – Sovat Jun 03 '13 at 03:40
  • @NOX, here is warning **Warning: session_start() [function.session-start]: Cannot send session cache limiter** – Sovat Jun 03 '13 at 03:41
  • @Sovat The dupe question's accepted answer has a section for UTF-8 BOM. – Fabrício Matté Jun 03 '13 at 11:47
  • @FabrícioMatté, Yes that page i used UTF-8. I have to use it because my site have japan language – Sovat Jun 03 '13 at 16:20
  • @Sovat The problem is not UTF-8. The problem is when you save the file in "UTF-8 with BOM" encoding which adds a couple extra bytes in the beginning of the file. There are many tools to remove the BOM, I personally use Notepad++. It is really well explained [linked answer](http://stackoverflow.com/a/8028987/1331430) (scroll down a bit to "UTF-8 BOM"). – Fabrício Matté Jun 04 '13 at 05:37

3 Answers3

0

Try placing this at the top of your page:

<?php ob_start(); ?>

then at the bottom of the page place this line of code:

<?php ob_flush(); ?>
Yogus
  • 2,307
  • 5
  • 20
  • 38
0

try using

<?php  ob_get_clean(); ?>

before the session_start. This way, you can be assured that no whitespace was printed before calling the output buffer sensitive function such as session_start() and/or header().

Aldee
  • 4,439
  • 10
  • 48
  • 71
-1

Try putting ob_start() after session_start()

Eric Evans
  • 640
  • 2
  • 13
  • 31