简体   繁体   English

为什么我不能在php脚本中使用session_start()? 它说标题已经发送

[英]Why can't I use session_start() in my php script? It says headers are already sent

Here are the first few lines of my page: 以下是我页面的前几行:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php include_once "dblogin.php";
session_start();
$loggedIn = 0;
if(isset($_SESSION["login"])) {$loggedIn = 1;}
?>

I'm getting the following error: 我收到以下错误:

Cannot send session cookie - headers already sent by (output started at /usr/www/users/simpleof/index.php:2) in /usr/www/users/simpleof/index.php on line 2 无法发送会话cookie - 已在第2行的/usr/www/users/simpleof/index.php中发送的输出(输出在/usr/www/users/simpleof/index.php:2处开始)

From what I've read on other forums, this should be fine because the session_start() is in the first block of php code, but I'm still getting this error. 从我在其他论坛上看到的,这应该没问题,因为session_start()在php代码的第一个块中,但我仍然收到此错误。

Here's how HTTP protocol works: 以下是HTTP协议的工作原理:

You send this kind of header with your browser: 您通过浏览器发送此类标题:

GET /questions/712326/why-cant-i-use-sessionstart-in-my-php-script-it-says-headers-are-already-sen HTTP/1.1
Host: stackoverflow.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fi; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fi-fi,fi;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://stackoverflow.com/questions/tagged/php
Cookie: *censored*
Cache-Control: max-age=0

First server sends you headers: 第一台服务器发送标头:

HTTP/1.x 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Expires: Fri, 03 Apr 2009 02:14:49 GMT
Vary: Accept-Encoding
Server: Microsoft-IIS/7.0
Set-Cookie: *censored*
Date: Fri, 03 Apr 2009 02:14:49 GMT
Content-Length: 9346

Then server sends you the actual page data 然后服务器会向您发送实际的页面数据

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >
<html>
<head>    

    <title>Why can't I use session_start() in my php script? It says headers are already sent. - Stack Overflow</title>
    <link rel="stylesheet" href="/Content/all.min.css?v=2743">
   ..snip..

So you see you can't FIRST send HTML data (DOCTYPE) and then header data because header is already processed. 所以你看到你不能首先发送HTML数据(DOCTYPE)然后标题数据,因为标题已经被处理。 You can go around with PHP's Output Control but more recommended is that you use MVC design where you buffer all data that user sees last. 您可以使用PHP的输出控件,但更多建议您使用MVC设计来缓冲用户最后看到的所有数据。

You either misread or the other forums are wrong. 你误读或其他论坛是错误的。 Just because session_start() is in the first "block" of PHP code doesn't mean it will work. 仅仅因为session_start()位于PHP代码的第一个“块”中并不意味着它将起作用。

session_start() needs to be run before the headers are sent. session_start()需要在发送标头之前运行。

The solution in your case is: 您的案例中的解决方案是:

Move session_start(); 移动session_start(); above <!DOCTYPE html ... 上面<!DOCTYPE html ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 警告:session_start():标头已发送时无法启动会话 - PHP Warning: session_start(): Cannot start session when headers already sent 为什么不能使用$ session_start()? - Why can't I use $session_start()? PHP警告:session_start():无法发送会话缓存限制器-标头已发送 - PHP Warning: session_start(): Cannot send session cache limiter - headers already sent session_start():无法发送会话缓存限制器-我的服务器中已发送的标头 - session_start(): Cannot send session cache limiter - headers already sent in my server 如何避免PHP session_start()与“ header has sent”消息已被catch 22警告? - How to avoid the catch-22 of PHP session_start() vs “headers already sent” warning? php session_start()脚本中无法获取$ _GET变量 - can't get $_GET variable in php session_start() script “session_start(): 无法发送 session 缓存限制器 - 标头已发送”即使在 session_start() 已经位于顶部之后也会继续出现 - “session_start(): Cannot send session cache limiter - headers already sent” keep coming even after session_start() already on top facebook.php错误-session_start()-标头已发送 - facebook.php error - session_start() - Header already sent 警告:session_start() [function.session-start]:无法发送会话缓存限制器 - 标头已发送 - Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent 为什么当我使用 session_start() 时我的 PHP 文件停止执行? - Why does my PHP file stop executing when I use session_start()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM