Hi Guys, Today I am going to tell you all possible way to fix the "Header already sent" error.
Headers already sent, error is the most common error in PHP.
Why “ Header already sent ” error is occurred?
Empty lines in your PHP code, is the main reason of this error. You faced this error specially when you are redirecting visitor from one page to other page using PHP header() function
. and mostly we used header() function
to redirect user to thank you page after submit form data.
"Header already sent"
error will always come when you left any empty line in your any PHP
file and including that file in top of page, In that case chances of this error is much more.
Now lets come on your question
How I can fix “Headers already sent” PHP error?
PHP Page Redirection code
- First check your page source and see that there should not any empty space lines before start the <!DOCTYPE html> tag.
- If space are there then, its clear that in your any header file, you have left any empty space lines, so you need to review top header file code and remove the empty space line, Your problem will be resolve.
- If you have not created code by yourself then don’t worry I will provide you other solution for that
- If you are facing “Header already sent” issue with any CMS like WordPress site then also you need to follow the sames steps, First review theme header.php file then review all theme code scripts those are including into header of the page using wp_head() hooks function
- As we have used many plugins on wordpress sites so might be any plugin code also can create this issues if they left empty space line in header top file.
<?php header("Location: https://www-site.com/"); exit; ?>
If are unable to fix the header already sent error using code then you can use given below code to add redirection
<?php echo '<script>window.location.href = "redirect code";</script>';exit; ?>