代码
代码如下 | 复制代码 |
<?php ob_start(); setcookie("username","宋岩宾",time()+3600); echo "the username is:".$HTTP_COOKIE_VARS["username"]."n"; echo "the username is:".$_COOKIE["username"]."n"; print_r($_COOKIE); ?> |
Warning: Cannot modify header information - headers already sent by出错的原因
我在php程序的头部加了,
header("cache-control:no-cache,must-revalidate");
原因分析
在PHP里Cookie的使用是有一些限制的。
1、使用setcookie必须在<html>标签之前
2、使用setcookie之前,不可以使用echo输入内容
3、直到网页被加载完后,cookie才会出现
4、setcookie必须放到任何资料输出浏览器前,才送出
.....
由于上面的限制,在使用setcookie()函数时,学会遇到 "Undefined index"、"Cannot modify header information - headers already sent by"…等问题,解决办法是在输出内容之前,产生cookie
解决办法
1在页面顶部的php标签中加入ob_start();
2在返回的信息下面加入ob_end_flush();
这样就可以屏蔽错误信息的现实了
但有的朋友说上面办法不行,后来我后来打开 php.ini 然后把 output_buffering 设为 on 。重起appache,OK。看来这才是解决办法。
另外有朋友说是编码问题,只要转换文档有uft-8就可以解决了,这里就不介绍了。
如果在执行php程序时看到这条警告:"Warning: Cannot modify header information - headers already sent by ...."
Few notes based on the following user posts:
有以下几种解决方法:
1. Blank lines (空白行):
Make sure no blank line after <?php ... ?> of the calling php scrīpt.
检查有<?php ... ?> 后面没有空白行,特别是include或者require的文件。不少问题是这些空白行导致的。
2. Use exit statement (用exit来解决):
Use exit after header statement seems to help some people
在header后加上exit();
header ("Location: xxx");
exit();
3a. Use Javascrīpt (用Javascrīpt来解决):
<? echo "<scrīpt> self.location( file.php );</scrīpt>"; ?>
Since it s a scrīpt, it won t modify the header until execution of Javascrīpt.
可以用Javascrīpt来代替header。另外需要注意,采用这种方法需要浏览器支持Javascrīpt.
3b. Use output buffering (用输出缓存来解决):
<?php ob_start(); ?>
... HTML codes ...
<?php
... PHP codes ...
header ("Location: ....");
ob_end_flush();
?>
另一篇文章
<?php
ob_start();
setcookie("username","宋岩宾",time()+3600);
echo "the username is:".$HTTP_COOKIE_VARS["username"]."n";
echo "the username is:".$_COOKIE["username"]."n";
print_r($_COOKIE);
?>
Warning: Cannot modify header information - headers already sent by出错的原因
我在php程序的头部加了,
header("cache-control:no-cache,must-revalidate");
之后页面就出现上面的错误,看了N个资料也没有结果。今天偶尔发现原来是我的php.ini里面的配置出了问题,在C:windows下找到php.ini文件
output_buffering默认为off的。
小提示,还有一个更好的解决办法就是在php.ini 然后把 output_buffering 设为 on [...]就不会出现这类问题了。
今天在运行phpmyadmin时页面突然出现了Fatal error: Out of memory (allocated 786432)提示,看英文的意思是说超过了指定内存大小了,下面我来介绍解决办法错误提示:
Fatal error: Out of memory (allocated 786432) (tried to allocate 393216 bytes) in E:xxxxlibrariescommon.lib.php on line 3135
解决办法
一,在你程序页面加入
代码如下 | 复制代码 |
ini_set('memory_limit',"256M") |
即可。
二,在php.ini中修改
打开php.ini 修改
代码如下 | 复制代码 |
memory_limit = 1024M |
的这行
依照服务器配置进行调整配置的内存使用大小修改后重启iis/apache
使用php header实现文件下载时,在IE6下总是提示不能下载,谷歌浏览器正常, 因此应该是IE的问题,IE下有个bug,微软官网有说明。错误消息:“Internet Explorer cannot download a file”(Internet Explorer 不能下载文件),有个办法可以很好的解决,看代码好了:
下面整理了一个函数
代码如下 | 复制代码 |
if(strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")==false) { header('Cache-Control: public, must-revalidate, max-age=0'); |
例
代码如下 | 复制代码 |
$ch = curl_init(); //初始化curl |
结果出现
Call to undefined function curl_init
解决方法如下:
1.打开php.ini,开启extension=php_curl.dll
2.检查php.ini的extension_dir值是哪个目录,检查有无php_curl.dll,没有的请下载php_curl.dll,再把php目录中的libeay32.dll,ssleay32.dll拷到c:windowssystem32里面
修改之后还有问题啊
在httpd.conf文件中加上:
LoadFile 动态链接库的完整路径
比如,此处php需要扩展curl,因此解决方法就是在httpd.conf文件中加上:
LoadFile d:/php/libeay32.dll
LoadFile d:/php/ssleay32.dll
这样就解决了