警告:session_start():无法发送会话,我已经尝试了所有可能的解决方案

时间:2014-12-10 07:24:40

标签: php html header

我尝试了一些来自互联网的解决方案,我将session_start();放在第一行,也尝试使用@ob_start();任何其他解决方案??? 我的代码在这里:

<?php
session_start();
require_once('page.inc');  

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>site 1</title>
<meta name="keywords" content="universe, blog theme, black, website template, templatemo" />
<meta name="description" content="About Universe, Our Company, free website template" />
<link href="templatemo_style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="js/jquery-1.6.3.min.js" ></script>


</head>
<body>
<?php
if(!isset($_SESSION['loggedinuser']))
{
if (isset($_POST['Signin']))
{
$username=$_REQUEST['username'];
if($username=='a')
$_SESSION['loggedinuser']=$username;
else
echo " the username or password you entered do not matchted ! ";

}
else
{
?>
 <form  method="post" action="index.php" >
<input type="text" name="username" id="username" > 
<input name="Signin" type="submit" value="Signin" />
</form>
<?

}
}
    else
    //something else
?>
    </body>
    </html>

这里的错误是:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /usr/local/apache2/htdocs/jasi/medu_quiz_22111_bl/index.php:1) in /usr/local/apache2/htdocs/jasi/medu_quiz_22111_bl/index.php on line 2

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /usr/local/apache2/htdocs/jasi/medu_quiz_22111_bl/index.php:1) in /usr/local/apache2/htdocs/jasi/medu_quiz_22111_bl/index.php on line 2

2 个答案:

答案 0 :(得分:1)

检查<?php之前的空格,确保<?php是第一个字符,而不是标签或间距。

答案 1 :(得分:0)

我已经尝试过您的代码并且一切正常,我复制粘贴它并且表单显示正常

甚至用正确的值提交表单两次,表单消失,一次输入错误的值并显示错误值

如果您之前放置了一些空格:

        <?php
        session_start();
        ....
        ?>

这可能会产生错误,在session_start()部分之前不应该出现任何问题,也许你已经注意到了页面:page.inc并且正在造成损害

这是我尝试的新代码,您可以采取以下步骤:

我已经用Opera检查了它,是的,首先它没有显示,但是它确实显示了大量刷新,尝试做一个没有缓存的机制,可能会将它发送到带有随机id的页面,这将清除缓存,问题不在于opera中的会话,会话正在运行,它在缓存中,

刷新你的歌剧你会,我把地址复制到歌剧的另一个标签上,会话显示,这是我的版本的完整副本:

        <?php
        session_start();
        require_once('page.inc');  

        ?>

        ........

        <?php
        if(!isset($_SESSION['loggedinuser']))
        {
            if (isset($_POST['Signin']))
            {
                $username=$_REQUEST['username'];
                if($username=='a')
                $_SESSION['loggedinuser']=$username;
                else
                echo " the username or password you entered do not matchted ! ";

            }
            else
            {
            ?>
             <form  method="post" action="index.php" >
                    ......
            </form>
            <?

            }
        }
        else if(isset($_SESSION['loggedinuser']))
        {
                $ses = $_SESSION['loggedinuser'];
                echo "LINE 47 session is $ses ";
        }
        else
        {
            $ses2 = $_SESSION['loggedinuser'];
            echo "LINE 51 session is $ses2 ";

        }
        ?>

            </body>
            </html>

这会产生:

第2行包括页面LINE 47会话是56

page.inc

        <?php
        echo " Line 2 included  a page ";
        ?>