0

I found the reason why I got this error, there was nothing wrong with my code, so I deleted it from my post.

My php page was encoded as UTF-8, but it had to be encoded as UTF-8 WITHOUT BOM (Byte Order Mark).

As I understand it, the BOM is interpreted as an output, or at least creates one.

Thanks for all of your answers, my question was a duplicate, I apologize for that, I looked at the other posts but not far enough. Thanks !

blex
  • 24,941
  • 5
  • 39
  • 72
  • The complete error message probably tells you when the headers were sent exactly (what file and what line). – jeroen Mar 15 '13 at 01:51
  • heres a tips use output buffering functions to know how to delay the script output. – Jhonathan H. Mar 15 '13 at 01:57
  • [...]headers already sent by (output started at /***/log.php:1) in /***/library.php on line 156 (log.php corresponds to the first block of code and line 156 is the line where I set the cookie) – blex Mar 15 '13 at 01:57
  • 1
    You mentioned that you have to be careful about extra "spaces" near the opening PHP tag. Did you make sure there are no line breaks or anything else before the opening PHP tag? Or extra whitespace before or after the PHP tags in library.php? – Travesty3 Mar 15 '13 at 02:04
  • Also, the while loop is unnecessary. It only executes when there is one row. So no need for a loop. – Travesty3 Mar 15 '13 at 02:10
  • I did check, no spaces, no line breaks, except right after the php starting tag of course. And you're right about the loop, I'll remove it. – blex Mar 15 '13 at 02:11
  • If your `library` include contains only php functions, you should remove the `?>` at the end, perhaps you have a space after that. – jeroen Mar 15 '13 at 02:11
  • while loop seems to be unecessary but it still don`t affect the behavior why headers are already sent... – Jhonathan H. Mar 15 '13 at 02:22
  • I agree, but I've always used a while loop to get the results. I don't know how to catch them without this loop... ^^ – blex Mar 15 '13 at 02:36

2 Answers2

0

In case you have done already HTML output prior the use of setcookie you need to find the place where your HTML output started. Above that line place the ob_start Docs function which will start output buffering.

With output buffering enabled, your program can still "output" HTML but it will not be send immediately. Then you should be able to call setcookie with no problems:

  <?php
  function login($username,$pwd)
    {   
        $mysqli=dbconnect(); //no output
        $query = "SELECT * FROM `users` WHERE `name`='$username' AND `password`='$pwd'";
        $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
        if($result->num_rows == 1) {
            while($row = $result->fetch_assoc()) {
                ob_start(); //set output buffering before setting cookies..
                setcookie("ID", $row['userid'], time()+3600*24*365);
                ob_end_flush(); // flush data
                if (isset($_COOKIE['UNIQUE_ID']))
                    {echo 'Connected';}
                else{echo 'No cookie today :(';}
            }
        }
        dbclose($mysqli); //no output
    }
    ?>

The output buffer will be automatically send to the browser when your script finishes, so there is not much more you need to care about, the rest works automatically.

Jhonathan H.
  • 2,734
  • 1
  • 19
  • 28
  • Sorry, not working. Do I need to 'flush' or something like that ? – blex Mar 15 '13 at 02:10
  • yes you may try to use any of those combinations of output buffering in my given links..Don`t forget whenever you start you must end .. So when you use ob_start() do flush the data.. – Jhonathan H. Mar 15 '13 at 02:17
  • try to use printf,echo,print to echo your set cookie if exist this would help if cookie has ben set... – Jhonathan H. Mar 15 '13 at 02:36
0

Try removing all white space before you're opening tag.

Closing tags are optional and may help with the misinterpretation of white space as output

hammus
  • 2,602
  • 2
  • 19
  • 37