php 解决总是输出Cannot send session cookie - headers already sent by错误的方法

PHP 1840 0 2014-07-09

php 解决总是输出Cannot send session cookie - headers already sent by错误的方法
非专勿扰!
一个页面内只有以下程序:
<?php
session_start(); 
$_SESSION['name']="店小二";
echo $_SESSION['name'];
?> 
这个页面再没有其它东西了,运行的时候提示如下:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at E:\xampplite\htdocs\vote\admin\login.php:1) in E:\xampplite\htdocs\vote\admin\login.php on line 2
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at E:\xampplite\htdocs\vote\admin\login.php:1) in E:\xampplite\htdocs\vote\admin\login.php on line 2
除了用error_reporting(0);还有什么办法不让提示错误。真正的不让警告。达到没有任何错误的目的。
-------------------------------------------------
清除一下bom头。bom清除函数如下:
<?php
//remove the utf-8 boms

 //by magicbug at gmail dot com

if (isset($_GET['dir'])){ //config the basedir

    $basedir=$_GET['dir'];

}else{

    $basedir = '.';

 } 

$auto = 1; 

checkdir($basedir);

function checkdir($basedir){

     if ($dh = opendir($basedir)) {

         while (($file = readdir($dh)) !== false) {

          if ($file != '.' && $file != '..'){

              if (!is_dir($basedir."/".$file)) {

                    echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>";

                }else{

                    $dirname = $basedir."/".$file;

                   checkdir($dirname);

                 }

            }

         }

    closedir($dh);

    }

 }

function checkBOM ($filename) {

    global $auto;

   $contents = file_get_contents($filename);

   $charset[1] = substr($contents, 0, 1); 

     $charset[2] = substr($contents, 1, 1); 

     $charset[3] = substr($contents, 2, 1); 
     if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {

         if ($auto == 1) {

             $rest = substr($contents, 3);

           rewrite ($filename, $rest);

            return ("<font color=red>BOM found, automatically removed.</font>");

        } else {

             return ("<font color=red>BOM found.</font>");

         }

     } 

    else return ("BOM Not Found.");

 }

function rewrite ($filename, $data) {

     $filenum = fopen($filename, "w");

     flock($filenum, LOCK_EX);

    fwrite($filenum, $data);

     fclose($filenum);

 }

?>

上一篇:php url编码函数浅析

下一篇:php如何在原来的时间上加1天?1小时?

讨论数量:0

请先登录再发表讨论。 2024-04-23

天涯网魂
3 杠 5 星
TA 的文章
TA 的随言
TA 的资源链