0

Possible Duplicate:
Headers already sent by PHP

hi im out of my league
i have written what has now become identical code for two sites hosted on different servers. the first worked perfectly and i have therefore used it to compare the second to.
from a login for i am directing to a loginaction.php when i include the db_connect.php ie

<?php session_start();
include 'db_connect.php';
$user=$_POST['formUser']
$password=$_POST['formPassword']

etc ? i am returning cannot modify header information header info already sent, but when i include the content of the db_connect ie

<?php session_start();
$connect = mysql_connect("localhost", "user", "pass");
mysql_select_db(db_name, $connection);
$user=$_POST['formUser']
$password=$_POST['formPassword']

etc ? it works.

any ideas?

edit - requested error message

Warning: Cannot modify header information - headers already sent by (output started at /home/avenncou/public_html/include/db_connect.php:4) in /home/avenncou/public_html/include/loginaction.php on line 14


at line 14 the is a
header("Location: {$_SERVER["HTTP_REFERER"]}");

edit - requested db_connect.php

<?php
    $connection = mysql_connect("localhost", "user", "pass");// or die ("Unable to connect!");   
    mysql_select_db("db", $connection);// or die ("Unable to select database!");  
?>


that is all of it (dies commented out in case thats where the error was)!!!

Community
  • 1
  • 1
alisdairv
  • 92
  • 2
  • 9

5 Answers5

3

remove ?> from db_connect.php

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • because PHP do not switch to HTML mode in this case and thus do not output trailing spaces to the browser, causing headers to be sent. – Your Common Sense Feb 01 '11 at 15:32
  • well thanks very much, i guess with learning the rules come learning when you can bend/break them. – alisdairv Feb 01 '11 at 15:32
  • You most likely had white space after the closing tag. *ANY* form of output before sending any type of header (which `session_start()` calls `header()`) results in a failure, unless output buffering is enabled. – Brad Christie Feb 01 '11 at 15:33
1

Maybe space after closing ?>

Just delete the last ?> in every file - PHP don't need that.

Marc
  • 6,749
  • 9
  • 47
  • 78
0

It may be something you're missing like a character before your <?php tag in this file, or any other files that were included before this piece was executed. (Some form of output to the client before session_start was called).

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • for this site there are only two files. an index.html only containing the form and the loginaction.php – alisdairv Feb 01 '11 at 15:02
  • 1
    Could we see the first 6 lines of db_connect.php (obviously redact any sensitive information first)? – ZoFreX Feb 01 '11 at 15:17
  • 1
    @outofmuleague_lucy: For testing purposes, place a `die('.');` immediately after the `session_start` and then view the page. Once loaded, do a view-source and see if anything shows up before the period you just forced to output. If there is, that/those character(s) are the cause of the problem. – Brad Christie Feb 01 '11 at 15:30
0

If you saved your file with Encoding UTF8 , try saving it with Encoding [UTF8 Without BOM] or with ASCII encoding .
Check that the files that include this file is [UTF8 without BOM] or ASCII encoded and that they have no spaces before session_start();

Also Check that the script does not echo or print anything before the session_start();

Rami Dabain
  • 4,709
  • 12
  • 62
  • 106
-1

Turn ON output buffer using ob_start() function.

Take a look ob_start()

To use ob_start you need to put it in the very first line of your program, sure you do not leave blank space before the ob_start() function.

I hope this help you.

devasia2112
  • 5,844
  • 6
  • 36
  • 56
  • Output buffering should NOT be used as a "bug fix". – binaryLV Feb 01 '11 at 15:24
  • why did you give me a down vote? – devasia2112 Feb 01 '11 at 15:25
  • all right, then you end up -- using ob_end_flush() – devasia2112 Feb 01 '11 at 15:26
  • Look, output buffering is a tool with it's certain purpose. To use it as a patch to cover lame mistake is a shame. Errors should be corrected, not patched. Don't you agree? – Your Common Sense Feb 01 '11 at 15:36
  • Well, yes, it's a horrible workaround. But not much worse than removing `?>`. So counterupvote +1 – mario Feb 01 '11 at 15:36
  • 1
    @mario nothing bad in removing closing tag. most coding standards already stated it as obligatory rule – Your Common Sense Feb 01 '11 at 15:38
  • I agree with you 100%. But you need to agre with me that code is to be done not copied. If you start the code from the begining, sure will avoid this kind of problem. Right? – devasia2112 Feb 01 '11 at 15:39
  • 1
    @Col.Shrapnel: I do not condemn removing them. And I'm aware that Zend recommends it. But nevertheless I consider it noob style. It's foremost an issue Windows coderz have due to flaky editing skills or editors. Bug again: I agree it's hence a good practice and recommendation. – mario Feb 01 '11 at 15:40
  • @Fernando Costa, about down vote - I didn't vote, it is from someone else. – binaryLV Feb 01 '11 at 15:46