- Ошибка «Warning: Cannot modify header information»
- Немного теории
- А теперь займемся удалением BOM
- How to Fix the “Cannot Modify Header Information – Headers Already Sent By” Error
- What Causes the “Cannot Modify Header Information – Headers Already Sent By” Error
- Get 4 Months Off
- How To Troubleshoot the “Warning: Cannot Modify Header Information – Headers Already Sent By” Error (2 Methods)
- 1. Fix the Error With the Plugin/Theme Editor or Replace a Plugin
- 2. Edit the Problem File via FTP/SFTP
- Summary
- Hand-picked related articles
- How to Fix a 403 Forbidden Error on Your Site
- How to Fix Error 404 Not Found on Your Site
- How to Fix The ERR_TOO_MANY_REDIRECTS Error
Ошибка «Warning: Cannot modify header information»
Часто при переносе сайта с локального компьютера можно встретиться с ошибкой вида:
Warning: Cannot modify header information - headers already sent by (output started at …
Причины такой проблемы различные. Одни из самых распространенных это:
C первой причиной все понятно — достаточно просто удалить лишние символы и проблема исчезнет.
А вот со второй проблемой намного интереснее.
Немного теории
BOM (англ. Byte Order Mark, BOM) — это метка порядка байтов Юникода, также её часто называют сигнатурой (соответственно, UTF-8 и UTF-8 with Signature).
По наличию сигнатуры программы могут автоматически определить, является ли файл закодированным в UTF-8, однако файлы с такой сигнатурой могут некорректно обрабатываться старыми программами, в частности xml-анализаторами. Многие программы Windows (включая Блокнот) добавляют байты 0xEF, 0xBB, 0xBF в начале любого документа, сохраняемого как UTF-8 — это и есть BOM.
А теперь займемся удалением BOM
Для того, чтобы удалить BOM из файлов, необходимо будет воспользоваться консолью (подключившись по SSH). Для подключения по SSH из Windows воспользуйтесь данной статьёй. Для поиска BOM‘а в файлах сайта, можно использовать команду:
$ find -type f|while read file;do [ "`head -c3 -- "$file"`" == $'\xef\xbb\xbf' ] && echo "found BOM in: $file";done
Команда выведет список файлов, в которых были найдены BOM-символы.
Также можно воспользоваться данной командой:
А с помощью нижеприведенной команды можно найти метки порядка байтов и сразу же удалить их:
$ find . -type f -exec sed 's/^\xEF\xBB\xBF//' -i.bak <> \; -exec rm <>.bak \;
Удачной работы! Если возникнут вопросы — напишите нам, пожалуйста, тикет из Панели управления аккаунта, раздел «Помощь и поддержка».
How to Fix the “Cannot Modify Header Information – Headers Already Sent By” Error
Most WordPress error messages give you an idea of what’s causing problems on your site. The “Warning: cannot modify header information – headers already sent by” error is no exception. If a PHP file cannot be executed due to a problem in its code, you’ll run into this message.
There are several potential causes for the “Cannot modify header information” error. Fortunately, the message itself will tell you which file is causing the problem. It even points to the line of code that contains the issue.
In this article, we’re going to discuss this error and its causes. Then, we’ll go over two ways that you can fix the problem. Let’s get to work!
What Causes the “Cannot Modify Header Information – Headers Already Sent By” Error
As we mentioned before, you’ll run into this error when one of your site’s .php files cannot be executed. WordPress relies on .php files, such as wp-config.php and functions.php, for its core functionality.
If there’s a problem within one of the .php files that your website needs to load, you’ll see an error message that looks like this:
Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/wp-config.php:#) in /home/public_html/wp-includes/file-example.php on line 33
Fortunately, the “Cannot modify header information” error provides a lot of information that makes troubleshooting relatively straightforward. The message will point you toward two files – the first one contains the problem, which prevents the second one from executing.
Get 4 Months Off
- Whitespaces before the segment of the code or after the closing ?> tag
- An HTML blockbefore the PHP header function
- print or echo statements added before the PHP header function
- Problems with a plugin’s code
Fixing these types of errors requires you to be at least passingly comfortable with modifying PHP code. You won’t need to add any code yourself.
Still, you may need a bit of extra help identifying the problem. This is particularly true if the issue isn’t related to whitespaces or statements before the PHP header function.
How To Troubleshoot the “Warning: Cannot Modify Header Information – Headers Already Sent By” Error (2 Methods)
There are two approaches to troubleshooting the “Cannot modify header information – headers already sent by” error. The first method doesn’t require you to exit the WordPress dashboard.
However, the second strategy uses FTP/SFTP if you can’t access the dashboard or use WordPress.
Let’s start with the first troubleshooting method.
1. Fix the Error With the Plugin/Theme Editor or Replace a Plugin
The first thing you need to do when you run into the “Cannot modify header information – headers already sent by” error is to open the file that’s causing the problem. Then, locate the line the message indicates.
For example, if you see an error that reads the following, it means you need to look inside your theme’s functions.php file:
Warning: Cannot modify header information - headers already sent by (output started at /home/public_html/wp-content/themes/twentytwentyone/functions.php:#) in /home/public_html/wp-includes/file-example.php on line 1
In this scenario, you can reach the source of the problem using the WordPress theme editor. To access it, go to Appearance > Theme Editor.
Once you’re in, use the menu to the right to select the file you need to access.
If you look closely, you’ll notice several whitespaces before the tag. The error message itself points toward line number one. Therefore, this tells you that the whitespaces are the sources of the problem.
In this example, all you have to do is remove the whitespaces and click on Update File. Now try reloading your website, and the error should be gone.
You can apply the same process using the WordPress plugin editor (Plugins > Plugin Editor). This method is applicable if the error message points toward a faulty plugin file.
Alternatively, you may run into an error that indicates one of the files within your WordPress plugins directory. In this scenario, you can remove and reinstall that plugin. In most cases, that will take care of the issue for you.
However, keep in mind that you might lose that plugin’s configuration, depending on which tool you use. As such, you may need to set up the add-on again.
2. Edit the Problem File via FTP/SFTP
In some cases, the source of the “Cannot modify header information – headers already sent by” error won’t lie in a file that you can access using the WordPress theme or plugin editors. Alternatively, you might be using a non-WordPress site.
In these scenarios, your best option is to access the problem file using FTP/SFTP. To do so, you’ll need to use an FTP or SFTP client such as the FileZilla platform.
You’ll also need access to your website’s FTP/SFTP credentials. In most cases, you should be able to find them within your hosting panel.
If you use Kinsta, you can access MyKinsta, select your website under Sites and click on its Info tab.
Once you have the credentials, use your FTP or SFTP client to connect to your website. You’ll need to locate the site’s root folder. Usually, its name should be root, public_html, public, or your own site’s name.
Here’s a quick look at what the inside of a WordPress root folder looks like.
Go ahead and locate the file that the “Cannot modify header information – headers already sent by” error indicates. For example, if the issue is public/wp-config.php , right-click on the file and select the View/Edit option.
That option will open the selected file using your default text editor. Once the document is open, locate the problem by navigating to the line the error message pointed you toward.
If you can’t spot the error, you might need to consult with someone who has experience working with PHP files. However, suppose you’re dealing with a whitespace issue or a statement before the PHP header. In that case, you should be able to fix the problem yourself.
Once you’re done, save the changes to the file and close the FTP/SFTP client. Try reaccessing your website, and the error should be gone.
Summary
The “Warning: cannot modify header information – headers already sent by” error can be intimidating because it outputs a long message. However, that detailed error message makes this bug relatively simple to troubleshoot. Unlike other problems, this one is polite enough to tell you which file is causing it and which line of code you need to look into.
Depending on the file that’s causing the error, there are two ways that you can go about troubleshooting it:
- Fix the error using the plugin/theme editor or replace a plugin.
- Edit the problem file via an FTP/SFTP client.
Finding the source of this error is simple. However, fixing it can be a problem if you’re not familiar with PHP.
Still having issues fixing this error? Please share your experience with our community in the comments below!
Save time and costs, plus maximize site performance, with $275+ worth of enterprise-level integrations included in every Managed WordPress plan. This includes a high-performance CDN, DDoS protection, malware and hack mitigation, edge caching, and Google’s fastest CPU machines. Get started with no long-term contracts, assisted migrations, and a 30-day money-back guarantee.
Check out our plans or talk to sales to find the plan that’s right for you.
Hand-picked related articles
How to Fix a 403 Forbidden Error on Your Site
The 403 Forbidden error indicates that the server understood the request but refuses to authorize it. Find out more about the causes and fixes.
How to Fix Error 404 Not Found on Your Site
The Error 404 Not Found status code indicates that the origin server did not find the target resource. Check out these common causes and fixes.
How to Fix The ERR_TOO_MANY_REDIRECTS Error
ERR_TOO_MANY_REDIRECTS is, as the error suggests, caused by too many redirects, resulting in a redirect loop. Read our post to learn more.