为什么我得到“无法修改标题信息”"未定义索引"错误


Why am I getting "Cannot modify header information" and "Undefined index" errors?

我有一个小的php脚本在服务器上工作良好,但不是本地,我尝试了很多,但似乎无法摆脱这些错误和登录:

Notice: Undefined index: page in /Users/myusername/Work/www/sabaqk/index.php on line 4
Notice: Undefined index: page in /Users/myusername/Work/www/sabaqk/class/account.class.php on line 26
Warning: Cannot modify header information - headers already sent by (output started at /Users/myusername/Work/www/sabaqk/index.php:4) in /Users/myusername/Work/www/sabaqk/class/account.class.php on line 68
Warning: Cannot modify header information - headers already sent by (output started at /Users/myusername/Work/www/sabaqk/index.php:4) in /Users/myusername/Work/www/sabaqk/class/account.class.php on line 69
Warning: Cannot modify header information - headers already sent by (output started at /Users/myusername/Work/www/sabaqk/index.php:4) in /Users/myusername/Work/www/sabaqk/class/account.class.php on line 70
Warning: Cannot modify header information - headers already sent by (output started at /Users/myusername/Work/www/sabaqk/index.php:4) in /Users/myusername/Work/www/sabaqk/class/account.class.php on line 71
Warning: Cannot modify header information - headers already sent by (output started at /Users/myusername/Work/www/sabaqk/index.php:4) in /Users/myusername/Work/www/sabaqk/class/account.class.php on line 72

我真的很感激任何帮助。谢谢!

正如我在注释中所述,Your web server probably has notices and warnings disabled in error reporting and your local host does not.由于您的本地主机启用了通知,因此这些通知错误将在脚本开头附近打印出来,这混淆了其余部分。当您尝试设置会话/cookie(无论您使用的是什么)时,PHP无法这样做,因为内容(您的错误)已经被发送,因此无法再设置cookie。

不要在你的PHP脚本中禁用错误报告,因为1)你不会修复潜在的问题,2)这些错误仍然会被发送到你的Apache错误日志,日志将成为一个巨大的文本文件,没有价值的信息。

这是一个非常简单的问题来解决。在尝试使用它们之前,只需检查您的密钥是否存在,如果它是一个数组,可以使用array_key_exists(),或者更简单,只需使用isset()。这将摆脱你的通知错误,并最终消除您的登录问题。

之所以发生这种情况,是因为error_display在服务器上必须关闭,而在本地系统上它是"打开"的。
如果你真的想以编程方式解决这些问题,请使用isset条件来检查错误日志中未定义的所有变量。

您可以包括以下两行来关闭警告和通知:

error_reporting(0);
ini_set('display_errors', false);