php session_start()出错原因分析及解决方法

yipeiwu_com6年前PHP代码库

错误提示: Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent

原因:在session_start()之前如果有输出内容,会出错,

解决办法:在session_start()之前加上ob_start();

index.php 

复制代码 代码如下:

<?php
error_reporting(-1);
ob_start();//不加会出错,无法写入session
register_shutdown_function('close');

 
echo 1;
 session_start();

$_SESSION['password']='mima2ddddddddddddddda2';

function close()
    {
        if(session_id()!=='')
            @session_write_close();
    }
?>
<a href="index2.php" >index2</a>

index2.Php
复制代码 代码如下:

<?php
error_reporting(-1);
ob_start();//不加会出错,无法读取session
?
echo 1;
 session_start();

echo $_SESSION['password'];
var_dump($_SESSION);
?>
<a href="index.php" >index</a>

相关文章

apache配置虚拟主机的方法详解

1.apache配置文件中打开vhost的配置LoadModule vhost_alias_module modules/mod_vhost_alias.soInclude conf/e...

Linux系统下php获得系统分区信息的方法

本文实例讲述了Linux系统下php获得系统分区信息的方法。分享给大家供大家参考。具体实现方法如下: $pars = array_filter(explode("\n",`df -h...

Apache连接PHP后无法启动问题解决思路

问题:apache之前正常,连接配置完PHP后无法启动,用apache Test Configration测试后报错形式为: Cannot load D:/php/php5apache2...

php调整gif动画图片尺寸示例代码分享

类的使用demo: 复制代码 代码如下:<?php require_once "roucheng.php";  $gr = new gifresizer; $gr->...

php 图片上添加透明度渐变的效果

复制代码 代码如下:<?php ////$strimgsrc = file_get_contents("/zb_users/upload/202003/e4lf1fxwbqa.jp...