0

I've a php script the first thing in the code is session_start();, but I keep receiving this error

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/****/public_html/admincp/index.php:1) in /home/****/public_html/admincp/index.php on line 1

but the script works great in the localhost.

<?php session_start();?>
<?php
require 'global.php';
?>


<?php

//check if the user logged in
if(!isset($_SESSION['user_id']))
page_redirect('login.php'); // go to login.php.
?>
Othman
  • 2,942
  • 3
  • 22
  • 31
  • 2
    If you’re using UTF-8, check your file for the byte-order mark (BOM) and remove it. – Gumbo Jun 25 '12 at 04:58
  • @meze the error on the line 1 not in the middle of the script. I have knowledge about making the session start in the beginning of the script. – Othman Jun 25 '12 at 05:00
  • @Othman the answer there has explanations for all cases, even if it happens on the first line. – meze Jun 25 '12 at 05:25
  • I've tried everything, still not working any help please :( – Othman Jun 25 '12 at 05:39
  • did you really try the answer from Gumbo? I also had once a problem with session_start and headers already sent... my source was saved as UTF8 "with BOM" (this is a invisible character in front of the document, but visible for php).. save it without bom and it should work. – MilMike Jun 25 '12 at 06:20
  • @qxxx how I can access these invisible characters I'm searching the web now to find something. – Othman Jun 25 '12 at 06:22
  • in your text editor there should be a function for this, in notepad++ for example in menu "Encoding / Convert to UTF8 without BOM" - some editors have this function in the save dialog. – MilMike Jun 25 '12 at 06:30

2 Answers2

2

Open the file in your favorite text editor and convert the file to UTF8 without BOM. Annoying error, once did the same thing.

Steen Schütt
  • 1,355
  • 1
  • 17
  • 31
0

A lot of times a lone whitespace character at the top of the file can cause this error, because the whitespace character gets output. Make sure that there's nothing above that <?php session_start(); ?>, not even a whitespace character. If the host you're on has an auto_prepend_file, that could also throw it off.

Sean Johnson
  • 5,567
  • 2
  • 17
  • 22