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?
- 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.
- You have tried all above options but still you are not able to find a solution to fix the Header already sent issue then you can use JavaScript instead of PHP header() function to redirect the user.
PHP Page Redirection code
<?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
JavaScript Page Redirection code
<?php echo '<script>window.location.href = "redirect code";</script>';exit; ?>