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>

相关文章

php中使用exec,system等函数调用系统命令的方法(不建议使用,可导致安全问题)

php的内置函数exec,system都可以调用系统命令(shell命令),当然还有passthru,escapeshellcmd等函数。 在很多时候利用php的exec,system等...

PHP使用CURL实现对带有验证码的网站进行模拟登录的方法

网上的很多模拟登录程序,大都是通过服务程序apache之类的运行,获取到验证码之后显示在网页上,然后填上再POST出去,这样虽然看起来很友好,但是既然模拟登录,登录后所干的事情就不一定是...

用PHP写的基于Memcache的Queue实现代码

php类代码: 复制代码 代码如下: <?php class MQ{ public static $client; private static $m_real; private...

基于PHP的cURL快速入门教程 (小偷采集程序)

基于PHP的cURL快速入门教程 (小偷采集程序)

最爽的是,PHP也支持 cURL 库。本文将介绍 cURL 的一些高级特性,以及在PHP中如何运用它。 为什么要用 cURL? 是的,我们可以通过其他办法获取网页内容。大多数时候,我因...

既简单又安全的PHP验证码 附调用方法

既简单又安全的PHP验证码 附调用方法

一、验证码示例 二、php验证码类,secoder.class.php <?php /** * 安全验证码 * * 安全的验证码要:验证码文字扭曲、旋...