search
HomeBackend DevelopmentPHP Tutorialsetcookie中Cannot modify header information-headers already s_PHP教程

Copy code The code is as follows:

setcookie("username","bu",time()+ 3600);
echo "aaaaa";
?>

运行有警告Warning: Cannot modify header information - headers already sent by 

下面是别人建议

Method 1:
There are some restrictions on the use of cookies in PHP.
1. Use setcookie must be before the tag
2. Before using setcookie, you cannot use echo to input content
3. The cookie will not appear until the web page is loaded
4. Setcookie must be placed before any data is output to the browser before it is sent
... ..
Due to the above restrictions, when using the setcookie() function, you may encounter problems such as "Undefined index", "Cannot modify header information - headers already sent by"..., the solution is Before outputting content, a cookie is generated. You can add the function ob_start() at the top of the program;

ob_start: open the output buffer
Function format: void ob_start(void)
Explanation: When the buffer is activated, all non-file header information from the PHP program will not be sent, but will be saved in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.


Method 2:
Solution to Warning: Cannot modify header information - headers already sent by ..... . A few days ago, I installed a php photo sticker system to test, and found that the error message Warning: Cannot modify header information - headers already sent by ......
I installed openads again today, and this error still occurred. question. Angry.After searching online for a long time, someone said to write
ob_start();
at the beginning of the file but failed.
Later open php.ini and set output_buffering to on. Restart appache, OK. It seems this is the solution.

Special note: (I solved the problem just after reading this)
If you use utf -8 encoding, you must remove the BOM in UTF-8. This is because the UTF-8 encoded file contains BOM, and php4 and 5 do not support BOM. To remove the BOM, you can open it with Notepad++ and convert it. (I solved the problem after reading this)

Solution by yourself

PHP4 and 5 do not support BOM in utf-8 encoded files

If the file contains BOM, an error will be reported when setting cookie or session.
How to quickly and easily remove the BOM of a file.
It is recommended to use Notepad++ software, a very excellent free software.
1. You can create a new UTF-8 (no BOM) in Settings-->Preferences-->New by default

2. If it is a newly created php file elsewhere, open in notpad++ how to convert the encoding to UTF-8 (no BOM)
Format-->UTF-8 (no BOM) Now you can transcode.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327005.htmlTechArticleCopy the code as follows: ?php setcookie("username","bu",time()+3600); echo "aaaaa"; ? There is a warning when running. Warning: Cannot modify header information - headers already sent by The following is...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
SpringBoot怎么通过Feign调用传递Header中参数SpringBoot怎么通过Feign调用传递Header中参数May 16, 2023 pm 08:38 PM

【SpringBoot】通过Feign调用传递Header中参数如何通过Feign传递Header参数问题描述我们在SpringCloud中使用Feign请求另一个服务的Api接口时,有将Header中参数传递下去的需求,如果不做特殊处理,就会将Header中的参数丢失。解决方案方案一:通过@RequestHeader(name="headerName")来传递例如:Feign定义如下@FeignClient(name="service-name")pub

linux的header是什么意思linux的header是什么意思Jul 18, 2023 pm 03:34 PM

linux的header是指在文件或数据流中的开头部分,用于包含关于内容的元数据,通过正确地编写和使用Header文件,开发者能够更好地利用系统资源,提高代码的可读性和可维护性。

如何使用PHP header()方法来调整网页如何使用PHP header()方法来调整网页Mar 28, 2023 pm 01:54 PM

PHP是一种功能强大的编程语言,可以用来创建动态网站和Web应用程序。其中一个最强大的功能之一是PHP的header()方法。在本文中,我们将探讨如何使用PHP的header()方法来调整网页。

php header头怎么实现跳转php header头怎么实现跳转Dec 02, 2022 am 09:14 AM

php header实现跳转的方法:1、使用“Header("Location:$url");”语法实现跳转;2、使用if判断式实现跳转,其跳转语句如“if($_COOKIE["u_type"]){ header('location:register.php'); } else{ setcookie('u_type','1','86400*360');”。

html5标签head和header有什么区别html5标签head和header有什么区别Jan 17, 2022 am 11:10 AM

区别:1、head标签用于定义文档头部,它是所有头部元素的容器,而header标签用于定义文档的页眉(介绍信息);2、浏览器都支持head标签,而旧版本浏览器均不支持header标签,需要IE9+以上浏览器才支持header标签。

如何在jQuery中添加、编辑和删除表格行?如何在jQuery中添加、编辑和删除表格行?Sep 05, 2023 pm 09:49 PM

在当今的Web开发时代,有效且高效的表管理变得非常重要,特别是在处理数据量大的Web应用程序时。从表中动态添加、编辑和删除行的能力可以显着增强用户体验并使应用程序更具交互性。实现这一目标的一种有效方法是利用jQuery的强大功能。jQuery提供了许多功能来帮助开发人员执行操作。表格行表格行是相互关联的数据的集合,由HTML中的元素表示。它用于将表格中的单元格(由元素表示)分组在一起。每个元素用于定义表中的一行,对于多属性表,通常包含一个或多个元素。语法$(selector).append(co

Nginx如何通过header中的标识进行分发Nginx如何通过header中的标识进行分发May 11, 2023 pm 04:01 PM

Nginx可以根据请求头中自定义的标识将请求分发到不同的服务器。具体来说,可以使用map指令将请求头中的自定义标识映射为不同的后端服务器地址,然后使用proxy_pass指令将请求转发到对应的后端服务器。以下是一个示例配置文件:http{map$http_my_header$backend{defaultbackend1.example.com;value1backend2.example.com;value2backend3.example.com;}upstreambackend1{serv

setcookie php 失效怎么办setcookie php 失效怎么办Oct 26, 2022 am 09:07 AM

setcookie php失效的解决办法:1、打开相应的PHP文件;2、查看域名参数是否规范;3、通过全局变量来实现即可,代码如“foreach ($_COOKIE["cookie"] as $name => $value) {$name = htmlspecialchars($name);$value = htmlspecialchars($value);echo...”。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

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),