IT牛人博客聚合
发现IT技术最优秀的内容, 寻找IT技术的价值首页 手机版 新闻 牛人列表 RSS订阅 关于
找到你所找, 得到你所想 - 即将推出IT牛人问答
0
0 ---php Cannot send session cookie – headers already sent by的解决方法
奇谭 发表于 2012年04月23日 17:20 | Hits: 108
Tag: 杂乱无章 | cookie - headers | php | session
今天在转移博客到另一个vps时遇到这个问题,提示的是“Warning : session_start() [function.session-start]: Cannot send session cookie – headers already sent by 。。。”,对php不熟悉,谷歌得到解决方法:
关于session的问题集锦解决方案
1.
错误提示
Warning: Cannot send session cookie – headers already sent
Warning: Cannot send session cache limiter – headers already sent
分析及解决办法
这 一类问题,的原因是你在程序中使用session_start()时,之前已经有实际的html内容输出了。或许你说,我没有啊,我只不过是echo或 print一条消息了。很抱歉,你的echo或print语句所产生的输出,就是实际的html内容输出。解决此类问题的办法是,将你的 session_start()调到程序的第一行。
2.
错误提示
Warning: open(F:/689\php\sessiondata\sess_66a39376b873f4daecf239891edc98b5, O_RDWR) failed
分析及解决方法
出现这样的错误语句一般是因为你的php.ini中关于session.save_path一项没有设置好,解决的方法是将session.save_path和session.cookie_path 设置置为
session_save_path = c:\temp
session.cookie_path = c:\temp
然后在c:\目录下建立一个temp目录,即可
3.
错误提示
Warning: Trying to destroy uninitialized session in
分析及解决方法
出 类这样的提示,一般情况都是你直接调session_destroy()函数造成的。很多的朋友认为session_destroy()函数可以独立的运 行,其实不然。解决的方法是在你调session_destroy()函数之前,要用session_start()开启session的功能。
4.问题:怎么获得当前session的id值呢?
最简单的方法是:
echo SID;
你会发现的。
5.问题:我的程序,在调用header函数之前没有任何的输出,虽然我include了一个config.php文件,但在config.php 文件中也没有任何的输出,为什么session还是会报出与问题1同样的错误呢,是不是因为我在header之前用了session_start()的缘 故呢?
答:或许你确实认真的检查了你的php程序,在引用header()之前确实也没有任何的输出,并且在你的include文件中也没有任 何的输出!但是你是否用光标键在?>这个PHP代码结束语句后移动检查呢?那么你会发现在?>这个后面,有一个空行或几个空格,你删除了这几 个空行或空格,那么问题就解决了。
注:此问题,会出PHP4.1.2中,更高版本,没有测试过。
6.问:用session做登录主页面后,其它页面怎么用session限制登录。。。
答:最简单的方法是
session_start();
if(!session_registered('login') ││ $login != true) {
echo "你没有登陆";
exit;
}
7.问:我用session_register()注册了session变量,可是当我用header或用javascript的重定向语句,那么在一下页面中,我却访问不到session所注册的变量值。请问如何解决?
问题的程序片段:
session_start();
$ok = 'love you';
session_register('ok');
header("location : next.php");
?>
next.php
session_start();
echo $ok;
?>
解决的方法:
当你用header函数或window.location这样的功能后,你上一个页面所注册的session变量,就会容易的丢失,关于这个问题的原因,至今仍没有一个详细的回答。
不过有解决的方法。如下所示 www.2cto.com
header("Location: next.php" ."?" . SID);
在跳转到下一页面的时候,将session的当前id做为一个参数,传到后一个页面。
8.session如何传数组
session_register('data');
$data=array(1,2,3,4);
方法是先注册后赋值
9.问题9:我是不是可以用像$HTTP_GET_VARS['**']方式来访问session值呢?
回答:可以,你可以使用如下global数组来访问session,以加强网页的安全性
$HTTP_SESSION_VARS
$_SESSION
例程:
session_start();
$username = 'stangly.wrong';
session_register('username');
echo $HTTP_SESSION_VARS['username'];
echo '
';
echo $_SESSION['username'];
?>
请参照此例程修改符合您自己的程序。
问题10:session_unregister() 和 session_destroy() 有何区别?
session_unregister() 函数主要作用是注消当前的一个session变量。不过要注意的是,如果你用$HTTP_SESSION_VARS或$_SESSION在当前页面中引用 过session变量,那么你可能需要和unset()配合 来注消session变量。
而session_destroy()是清除当前的 session环境。意思就是说,当你用session_destroy()函数后,那么你就不可能再用session_is_registered() 来检测session的变量了。但是需要注意的是他不能清除global中的session或使用了session cookie的中的session.所以在用session_destroy之前,最好不要用$HTTP_SESSION_VARS $_SESSION来访问session.(译自于php.net)
例程:
if(isset($_COOKIE[session_name()])) {
session_start();
session_destroy();
作者 奇谭

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
