【问题标题】:: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\rail\index.php:446) in [duplicate]: session_start(): 无法发送会话缓存限制器 - 标头已发送(输出开始于 C:\xampp\htdocs\rail\index.php:446)在 [重复]
【发布时间】:2016-01-10 16:24:59
【问题描述】:

这是警告。即使它不允许我打开页面 admin.php 并且错误是“:

无法发送会话缓存限制器 - 标头已发送(输出开始于 C:\xampp\htdocs\rail\index.php:446)在

    <?php

    ob_start();

    error_reporting(E_ALL); 

此行有警告。我开始会话的地方

     session_start();
    include("db.php");
    if(isset($_POST['log']))
      {

   $user= $_POST['username'];
    $pass= md5($_POST['password']);

   $sql=mysql_query( "select * from reg where username= '$user' AND password='$pass' ") or die( mysql_error());
   $data=mysql_num_rows($sql);
      if ($data == 1) {
   while($row = mysql_fetch_array($sql)){ 

     $_SESSION['username']=$s1;
   echo '<script>window.location="admin.php"</script>';
         }
        }


      else {
        echo '<script type="text/javascript">';
          echo 'alert("Password Invalid!")';
         echo '</script>';

        }
       }
       ob_end_flush();
       ?>

enter image description here

【问题讨论】:

  • PLZ 给出一些解决方案!!!!... 热切等待
  • Index.php 在第 446 行之前一两行,错误信息再明显不过了。 .如果失败了,这个问题已经被问过很多次了。查看本题右侧的相关栏目。
  • 我看过了。但没有找到任何有用的解决方案。请帮我离开这里。 @DarylGill
  • 在我输入之前。所以你知道什么可能导致这个警告吗?
  • 是在服务器上运行还是在本地运行

标签: php session session-variables php-5.5


【解决方案1】:

简而言之。有很多事情可能导致此错误/警告。其中大部分被缩小到在开始会话之前发送到浏览器的一段输出。因此,考虑到这一点,请检查脚本中的输出。在初始化会话之前,这可以是简单的空白或文本。

显示此警告的另一种常见情况是由您的文件编码触发。可能有一个叫做 UTF8-BOM 的东西,它是一个隐藏字符,一些编辑器可能不会选择它,前提是你为你的文件设置了错误的编码。所以在这种情况下,解决方案是仔细检查文件编码。

错误消息很清楚,它指出 index.PHP 和大约第 446 行是此警告的大致原因。所以检查我上面提到的在那一行或之前提到的事情

【讨论】:

  • 好的@daryl gill.......
  • 不,那个空间也不起作用,兄弟。
  • @komaldeepchahal 我为该问题提供了 cmets 中另一个问题的链接。检查一下,答案还提供了用于缩小错误位置的代码的 sn-p。
  • @deryl 你能不能通过teamviewer来电脑上……如果你不介意
  • 好的,让我检查一下。
【解决方案2】:

尝试使用它可能会对您有所帮助,

 <?php
 session_start();
ob_start();
error_reporting(E_ALL); 
include("db.php");
include("redirect.php"); // the file which is stored redirect()   
 if(isset($_POST['log']))
  {

 $user= $_POST['username'];
 $pass= md5($_POST['password']);

  $sql=mysql_query( "select * from reg where username= '$user' AND password='$pass' ") or die( mysql_error());
  $data=mysql_num_rows($sql);
  if ($data == 1) {
    while($row = mysql_fetch_array($sql)){ 

 $_SESSION['username']=$s1;
 redirect('admin.php');            // HERE YOU NEED TO REPLACE THE FUNCTION
     }
    }
    else {
    echo '<script type="text/javascript">';
      echo 'alert("Password Invalid!")';
     echo '</script>';

    }
   }
   ob_end_flush();
   ?>

如果您的代码中没有任何 html 标签,请避免使用 ?&gt;

编辑:

如果上述方法对您没有帮助,请尝试使用下面的 function 而不是 header()

  function redirect($url) {
     if (!headers_sent()) {    
      header('Location: '.$url);
      exit;
  } else {  
    echo '<script type="text/javascript">';
    echo 'window.location.href="'.$url.'";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
    echo '</noscript>'; exit;
  }
}

感谢Rajat Singhalredirect()

【讨论】:

  • 我认为提供的代码没有边际差异。如果这是复制和粘贴。由于&lt;?php之前的空格,它很可能会触发错误
  • 好的,让我看看这个人
  • @Daryl Gill,亲爱的,看看我的编辑
  • 为什么这对运营有利?没有提供学习曲线,有点离题,因为它基本上没有回答
  • 嘿@VishnuRNair 你能告诉我我把这个函数放在我的代码中的什么地方吗.. 以及我替换它的地方
猜你喜欢
  • 2016-12-14
  • 2018-01-29
  • 2016-05-28
  • 2013-09-11
  • 2020-07-02
  • 1970-01-01
  • 2014-06-04
  • 2012-02-07
相关资源
最近更新 更多