开发者

the sessions in php [duplicate]

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

hello I just finished the development of a website in php I host the site and got the following error

Warning: session_sta开发者_JAVA百科rt () [function.session-start]: Can not send session cookie - headers already sent by (output started "at / home/qualitr1/public_html/DemoEcolace/index.php: 2) in / home/qualitr1/public_html/DemoEcolace / index.php on line 2

knowing that I had not this problem locally on windows the code for my file is as follows:

<? Php
session_start ();
?>


the reason why this is happening is because sessions use cookies, and cookies are transferred via http headers, you cannot send headers if you have already sent content to the web page

your php ini specifies that short tags is on, there for instead of using <?php your able to use <? instead.

as your doing:

<? Php
session_start ();
?>

your page is probably spitting out some error text because if the space between <? and php there for session start cannot be initiated.

you should always make sure that your not sending content before you call sessions_start()

you should do:

<?php
session_start();
?>

Making sure there is no content before or after the php tags.

also as mentioned by another post there's the possibility of a hidden char called BOM, (Byte order mark), this character is invisible and cannot be seen but may exists.

if your using a notepad such as notepad++ then you can select the Format then UTF-8 Without BOM.

if the above does not resolve the issue then you can create a small script to scan your application for BOM chars:

just find some recursive tool and then for each php file you find you can do:

$bom = pack("CCC", 0xef, 0xbb, 0xbf);

and then check the first 3 chars of the file like so:

if (0 == strncmp($str, $bom, 3))
{
    //this file has BOM Char
}


First of all, there should not be a space between the <? and php. Also, check the beginning of your file for UTF-BoM (Byte order mark). It's invisible in text editors, but it still counts as output hence causing errors like the one above.


There can be no outputs before starting a session. Maybe there's an output in index.php, line 2. If your document is encoded in UTF-8, your php file must be encoded in "UTF-8 without BOM".


Sending a cookie requires to modify the HTTP header. And that is only possible if it hasn’t been sent yet. And that happens in general when the output starts (either explicitly or implicitly).

So in order to use a cookie for the session, you need to make sure that there was no output before calling session_start or that the output was not yet sent to the client (see output control).

According to the error message, the output started at /home/qualitr1/public_html/DemoEcolace/index.php:2. So take a look at what happens in that line and prevent that output (including whitespace like line breaks, spaces, tabs as well as a possible UTF BOM) or use the output control to buffer any output so that you can modify the HTTP header even if there already was some output.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜