开发者

problem with session_start() [function.session-start]: [duplicate]

This question already has answers here: How to fix "Headers already sent" error in PHP (11 answers) Closed 10 years ago.

here's the error code :

Cannot send session cookie - headers already sent by (output started at开发者_运维百科
C:\xampp\htdocs\log\New folder (4)\New folder (2)\iskono\forum.php:5) in 
C:\xampp\htdocs\log\New folder (4)\New folder (2)\iskono\forum.php on line 244

Warning: session_start() [function.session-start]: Cannot send session cache limiter - 
headers already sent (output started at C:\xampp\htdocs\log\New folder (4)\New folder 
(2)\iskono\forum.php:5) in C:\xampp\htdocs\log\New folder (4)\New folder 
(2)\iskono\forum.php on line 244 

line 244 is the require code

<? require("forum/index.php"); ?>

how solve this error?


You need to put your session_start call before any output (HTML, for example), is sent. So put it above your doctype declaration and make sure there is no whitespace before. For example:

<?php
    session_start();
?>
<!DOCTYPE html>
<html>
...


You have obviously sent some data before calling session_start(), this must be called at first place!

Right

<?php
session_start();
echo "hi man :) "; // this is right

Wrong

<?php
echo "hi man :( ";  //this is wrong
session_start();


Since no one else has mentioned it yet: If you are encoding your files (NOT output of your files, the files themselves) in UTF, make sure you do so without a byte order mark (UTF-8 without BOM).

Adding the BOM at the beginning of the file can cause this error, since it counts as output.

Additionally, you can use output buffering to solve any other issue relating to 'headers already sent' errors. This causes no output to be sent until you explicitly say 'send output' with ob_flush() or ob_end_flush().


You have done one of the following:

  • Called echo, print or some other function that causes output on line 5.]
  • include or required a file that has caused output to be sent. Check all you included files for leading/trailing whitespace before/after the <?php ?> tags.
  • Done something else that has cause an error message to be written to output on line 5.

What is on line 5?


In php, A header can be send before any other output is sent. If you have sent any output before header, your header will generate an error. (even a space before the


Make sure you aren't sending any characters to the browser before the session starts.

It looks like you're sending something on line 5 of forum.php.

The could be due to an echo, print or because you're sending text directly from your script:

<?php
  some code....
?>
text sent to the browser
<?php
  more code.
  session_start();
?>

Ensure that the session starts early by putting session_start(); at the beginning of your script.

Many others have had similar problems with sessions. Take a look at the PHP manual regarding session_start() at http://php.net/manual/en/function.session-start.php for session problems, solutions and tips.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜