Warning: Cannot modify header information - headers already sent
Happens when your script tries to send an HTTP header to the client but there already was output before, which resulted in headers to be already sent to the client.
This is an E_WARNING
and it will not stop the script.
A typical example would be a template file like this:
<html>
<?php session_start(); ?>
<head><title>My Page</title>
</html>
...
The session_start()
function will try to send headers with the session cookie to the client. But PHP already sent headers when it wrote the <html>
element to the output stream. You'd have to move the session_start()
to the top.
You can solve this by going through the lines before the code triggering the Warning and check where it outputs. Move any header sending code before that code.
An often overlooked output is new lines after PHP's closing ?>
. It is considered a standard practice to omit ?>
when it is the last thing in the file. Likewise, another common cause for this warning is when the opening <?php
has an empty space, line, or invisible character before it, causing the web server to send the headers and the whitespace/newline thus when PHP starts parsing won't be able to submit any header.
If your file has more than one <?php ... ?>
code block in it, you should not have any spaces in between them. (Note: You might have multiple blocks if you had code that was automatically constructed)
Also make sure you don't have any Byte Order Marks in your code, for example when the encoding of the script is UTF-8 with BOM.
Related Questions:
What you're missing is running composer install
, which will import your packages and create the vendor folder, along with the autoload script.
Make sure your relative path is correct. For example the example scripts in PHPMailer are in examples/
, below the project root, so the correct relative path to load the composer autoloader from there would be ../vendor/autoload.php
.
The autoload.php you found in C:\Windows\SysWOW64\vendor\autoload.php
is probably a global composer installation – where you'll usually put things like phpcs, phpunit, phpmd etc.
composer update
is not the same thing, and probably not what you want to use. If your code is tested with your current package versions then running update
may cause breakages which may require further work and testing, so don't run update
unless you have a specific reason to and understand exactly what it means. To clarify further – you should probably only ever run composer update
locally, never on your server as it is reasonably likely to break apps in production.
I often see complaints that people can't use composer because they can't run it on their server (e.g. because it's shared and they have no shell access). In that case, you can still use composer: run it locally (an environment that has no such restrictions), and upload the local vendor folder it generates along with all your other PHP scripts.
Running composer update
also performs a composer install
, and if you do not currently have a vendor
folder (normal if you have a fresh checkout of a project), then it will create one, and also overwrite any composer.lock
file you already have, updating package versions tagged in it, and this is what is potentially dangerous.
Similarly, if you do not currently have a composer.lock
file (e.g. if it was not committed to the project), then composer install
also effectively performs a composer update
. It's thus vital to understand the difference between the two as they are definitely not interchangeable.
It is also possible to update a single package by naming it, for example:
composer update ramsey/uuid
This will re-resolve the version specified in your composer.json
and install it in your vendor folder, and update your composer.lock
file to match. This is far less likely to cause problems than a general composer update
if you just need a specific update to one package.
It is normal for libraries to not include a composer.lock
file of their own; it's up to apps to fix versions, not the libraries they use. As a result, library developers are expected to maintain compatibility with a wider range of host environments than app developers need to. For example, a library might be compatible with Laravel 5, 6, 7, and 8, but an app using it might require Laravel 8 for other reasons.
Composer 2.0 removed any remaining inconsistencies between install and update results; if you're running composer 1.x you should definitely upgrade.
Best Answer
Small mistake: There is no folder named "Sourcescode".
Look closely, there is a space in between.
Give the correct address when you're including the file. That'll fix the error.
Since there is no folder named "Sourcescode", php is unable to find the Mail.php file.