I get this warning that doesn't allow me to continue:
Warning: session_start(): Cannot start session when headers already sent in C:\xampp\htdocs\bancoinfocial\index.php on line 45
Notice: Undefined variable: _SESSION in C:\xampp\htdocs\bancoinfocial\principal.php on line 3
And this is my code:
index.php
<div id="principal" hidden>
<?php
session_start();
require_once("principal.php");
?>
</div>
main.php
<div>
<?php
echo $_SESSION['fullname'];
?>
</div>
That is, I call principal.php from index.php where it is not possible to log in due to headers already sent, even though it is the first instruction that I execute in each invocation of the method session_start()
.
session_start()
is a function that sends various HTTP headers depending on the configuration, so it cannot be executed after content has been written (because then the headers cannot be modified).You have
<div>
in your HTML code that precedes thesession_start
PHP code and that is what is causing the problem.session_start
should come first, before anything is written. So you should move it to the top of everything to avoid that error:The problem is because you are doing a
div
in your html before validating the session. In PHP thesession_start();
must come before anything else. Even before himdiv
.Try the following:
index.php
For it to work you have to move the session session_start to the beginning of the index.php.
Index.php
main.php
The same thing happened to me, try doing this:
solution 1
if it doesn't work for you, it could be that a native function that you use from php, that is creating a session or something like that and doesn't allow you to include a file in my case was the following.
This ("base64_encode()" did not allow me to include a file with php where it will start a session.
Solution 2
the solution seems complex but it is not, you just have to create a container where you are going to include everything, and you include it with Javascript and ajax like this:
To make it load at the beginning of the page you must put it like this:
solution 3
yes, lastly, if you include this line
session_start();
in the php file that you include and where it includes them, that is, in the parent and in the child, the solution is just to leave it in a file, and you must leave it where you need to use this global variable$_SESSION
.I had the same problem and it was a simple space prior to the call of the session... Very good the observation of another colleague, because I would not have believed it!
It works like this:
possibly the problem is in the version of PHP that you are using. If you have PHP 7.2, downgrade it to 7.1 which is a much more stable version.
It also enjoys most of the great advances that 7.2 has, especially the speed of execution.
I had the same problem when I changed cPanel from version 5.4 to 7.2, when I changed it to 7.1 everything returned to normal.
If there is no compelling reason to use version 7.2 switch to 7.1.
Hope this helps.
I had the same error!! Locally it worked perfectly for me but when uploading it I was presented with something similar to this: Warning: session_start(): Cannot start session when headers already sent... but I solved it by updating the PHP version I was using 7.3 and down to 7.1 and this is how i was able to fix the error.