标题已发送,找不到问题

时间:2012-09-12 21:56:25

标签: php session header

  

可能重复:
  Headers already sent by PHP

好的,所以我制作了一个有管理面板的脚本,一切运行良好。我移动了服务器,现在代码中断了。我一直在谷歌搜索,并且在任何打开/关闭php标签之前没有空格,我有ob_start(); &安培; ob_end_flush()函数;在session_start()之前和之后,我仍然遇到错误。任何人都可以找到我出错的地方吗?

<?php
    ob_start();
    $host="localhost"; // Host name
    $username=""; // Mysql username
    $password=""; // Mysql password
    $db_name=""; // Database name 
    $tbl_name=""; // Table name
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    $ref = null;
    if ($_POST['ref'])
    {
$ref = $_POST['ref'];
    }
    $myusername=$_POST['username'];
    $mypassword=$_POST['password'];
    $myusername = stripslashes(mysql_real_escape_string($myusername));
    $mypassword = stripslashes(mysql_real_escape_string($mypassword));
    $sql="SELECT * FROM `$tbl_name` WHERE `username`='$myusername' and           `password`='$mypassword'";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
    if($count==1){
    $dt2=date("Y-m-d H:i:s");
function getip()
{
    $ip = null;
        if (isset($_SERVER["REMOTE_ADDR"])){$ip = $_SERVER["REMOTE_ADDR"];} 
        else if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){$ip =     $_SERVER["HTTP_X_FORWARDED_FOR"];}
        else if (isset($_SERVER["HTTP_CLIENT_IP"])){$ip =   $_SERVER["HTTP_CLIENT_IP"];}
    return $ip;
}
$myperms = null;
while ($row = mysql_fetch_array($result)) {
    $myperms = $row['perms'];
    }
     mysql_query("UPDATE admins SET `lastlogin`='".$dt2."',`lastip`='".getip()."', state='1' WHERE username='".$myusername."'"); 
     mysql_query("UPDATE settings SET `lastadmin`='".$myusername."',`lastadminip`='".getip()."'"); 
    session_start();
    $_SESSION['username'] = $myusername;
    $_SESSION['permissions'] = $myperms;
    if ($ref)
    {
    header("location:admin.php?list=".$ref);
    }
    else
    {
    header("location:admin.php");
    }
    }
    else {
    echo "Wrong Username or Password";
    }
    ob_end_flush();
    ?>

我不断收到2个错误(主目录路径已被淘汰):

    Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at admin/checklogin.php:1) in admin/checklogin.php on line 47

    Warning: Cannot modify header information - headers already sent by (output started at admin/checklogin.php:1) in admin/checklogin.php on line 56

我无法弄清楚为什么它会在新服务器上执行此操作而不是旧服务器。谷歌上的另一篇文章称“有些主机在发送标题之前不喜欢html”,但我在这个文件中没有任何HTML,所以我仍然不知所措。任何想法?

提前致谢。

1 个答案:

答案 0 :(得分:4)

即使在你的问题中,我也会在<?php之前看到空格。 <?php之前的任何内容都被视为输出,您在发送header之前不应发送任何内容

如果不是<?php之前的4个空格 - 请在文件中查找BOM字符。