Problem with session_start () in php.

1

I am having a problem that is driving me crazy and I can not see the fault. The good thing is that I managed to reproduce the fault in a very simple situation.

Let's see, I have two php files. The first, index.php is like this:

<?php session_start(); ?>
<!doctype html>
<html lang="es-ES">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Chk login</title>
    <script>
                    function hacerPost(ruta, params){
                        var body = document.body;
                        form = document.createElement('form');
                        form.method = 'POST';
                        form.action = ruta;
                        form.name = 'jsform';
                        for (var x = 0; x < params.length; x++) {
                            var input = document.createElement('input');
                            input.type = 'hidden';
                            input.name = params[x][0];
                            input.id = params[x][0];
                            input.value = params[x][1];

                            form.appendChild(input);
                        }
                        body.appendChild(form);
                        form.submit();
                    }
                </script>
</head>
<body>
    <?php
        $_SESSION["gestor"]="gestor123";
        $_SESSION["rol"]="rol456";
        ?>
    <script>
        hacerPost('./prueba_sesion.php',[["idgestor","1000"]]);
    </script>

</body>

This file creates two sessions, the session "manager" and the session "role" and then passes one parameter per post (in this example the parameter is "idgestor" and is sent by the function hacerPost ()) to the second php file that must receive the post parameter and the two sessions.

This second file, prueba_sesion.php is:

<?php session_start();
echo "===================================<br>";
echo "Sesion gestor: ". $_SESSION["gestor"]."<br>";
echo "Sesion rol: ".$_SESSION["rol"]."<br>";
echo "Post idgestor: ".$_POST["idgestor"]."<br>";

Running on localhost with XAMPP works perfectly and displays the values of the sessions ("manager123" and "rol456") on the screen. However, and here the problem, in the server does not work, it does receive the post parameter correctly, but the sessions do not and in return, I receive the error (in a file error.log):

[09-Nov-2018 23:21:11 Europe/Madrid] PHP Warning:  session_start(): Cannot start session when headers already sent in ..../pruebas/inicio_sesion/prueba_sesion.php on line 1

I have been reviewing typical questions in this regard and the most common error is not to put session_start () first, but I think that is not the case, because it is on the first line and I have verified that there are no strange characters before. And there is the paradox that it does work on localhost.

Both on localhost and on the server I have php 7.2. I have tried on 2 different servers and it does not work.

Any ideas?

    
asked by AngelF 09.11.2018 в 23:50
source

0 answers