session_start() error - Not below any other ouput

Status
Not open for further replies.

andypalmer86

New Member
Messages
4
Reaction score
0
Points
0
I have a session based login system on my site which worked fine on another server but I'm getting these errors on this server:

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

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/andy86/public_html/index.php:1) in /home/andy86/public_html/index.php on line 2

I've searched the forums for similar problems but everyone i've seen has been because the person had output before the session_start() command...however my index.php file is like this:
HTML:
<?php
    session_start();

    if (isset($_POST['logout']))
    {
        $_SESSION = array();
        if (isset($_COOKIE[session_name()])) {
                setcookie(session_name(), '', time()-42000, '/');
        }

        session_destroy();
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN">
....
Does anybody know what could be causing these errors? I'm guessing it could be the .htaccess file because I don't have any experience with that so I don't really know what it does :dunno:
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
Are you sure you don't have a space or newline before the opening php tag? Also, this file isn't being include()'d, right? Look at the html source to see if there's anything before the first warning. A php warning begins with a <br />.

If you're absolutely sure that there's no output there, then it *might* be a control character. If your editor has a hex mode, you can switch to it to see if there's any invisible character before the opening php tag. Otherwise just make a new file and start it with <?php, then copy everything in index.php after the opening php tag to the new file and save it as the new index.php.
 

andypalmer86

New Member
Messages
4
Reaction score
0
Points
0
Now that you mention it I was getting a warning on the W3C validator about a Byte-Order Mark, I think that could be the problem.....I'll download Notepad++ again to remove it and see what happens. Thanks for that!
 

phpasks

New Member
Messages
145
Reaction score
0
Points
0
You can use it now
<?php
ob_start();
session_start();

if (isset($_POST['logout']))
{
$_SESSION = array();
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}

session_destroy();
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN">
....
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
You can use it now

ob_start() is a bite late there...
PHP:
{output something}?
<?php
//Send a header
//Contents - after header
?>

changed into
PHP:
{output something}?
<?php
//buffer output BEHIND THIS LINE
//send a header - unbuffered
//Contents - buffered - after header
?>

Difference: 0

Reason: the php is not responsible for this error. i'm 99% certain there's some sort of character before the <?php
(newlines, control characters -> try saving the file as ASCI, ...)
 

andypalmer86

New Member
Messages
4
Reaction score
0
Points
0
Hello, thank you all for your interest in this problem. It was a UTF-8 Byte-Order Mark causing the problem, which from what i've read is an invisible character used by old editors to idenity the file as UTF-8 encoded.

I downloaded Notepad++ as it has an option to remove the BOM from files and session_start() is now working perfectly :)
 

tnl2k7

Banned
Messages
3,131
Reaction score
0
Points
0
I'll close this thread then :).

-Luke.

* Thread Closed *
 
Status
Not open for further replies.
Top