Problems with session_start (): Can not send session cache limiter

1

I'm doing a login using the MVC model in PHP and I have a problem of

  

Warning: session_start (): Can not send session cache limiter - headers already sent (output started at C: \ xampp \ htdocs \ backEndEmisoraSanJoseObrero \ views \ modules \ navegacion.php: 1) in C: \ xampp \ htdocs \ backEndEmisoraSanJoseObrero \ controllers \ controller.php on line 43

and

  

Warning: Can not modify header information - headers already sent by (output started at C: \ xampp \ htdocs \ backEndEmisoraSanJoseObrero \ views \ modules \ navegacion.php: 1) in C: \ xampp \ htdocs \ backEndEmisoraSanJoseObrero \ controllers \ controller .php on line 42 **

When wanting to work with the sessions

The Form is this:

<center>
	<div class="admin">
		<h1 class="ingresoTitle">Inicio de Sesion del Administrador</h1>
		<form method="post">
				<input type="text" placeholder="Usuario" name="usuarioIngreso">
				<input type="password" placeholder="Contraseña" name="passwordIngreso">
				<input type="submit" value="Enviar">
		</form>	
	</div>	
</center>

<?php  
$ingreso = new MvcController(); //Creamos un nuevo objeto
$ingreso -> inicioSesionController(); //llamamos la funcion
?>

And the PHP code that handles the form this:

public function inicioSesionController(){


    if (isset($_POST['usuarioIngreso'])) {

        if ($_POST['usuarioIngreso'] == 'admin') {
            header('location=ok');
            session_start();
            $_SESSION['user']= true;
        }

    }
}
}

I have been looking at my code for days, changing and testing and I can not find the problem.

To enter the form, use a GET link that is ?action=admin

The full code on GitHub: link

The weird thing is that this login uses the same technique and does not throw error link

    
asked by Darguello 22.11.2017 в 23:19
source

1 answer

1

Regarding your code:

  • If you are going to work with sessions, and your php.ini does not have session.auto_start enabled, you yourself initialize the session and it is one of the first things you do. You do not do it within a method.
  • if you print text (in your case you print the HTML first) you can not modify the headers afterwards. The headers were implicitly sent as soon as you entered a character. Same things with the sessions, you can not get a hand after the headers were sent.
  • header('location=ok') intrigues me. Is it an arbitrary code that you want to then inspect from the front? Because if you wanted to redirect, then the syntax would be header ('Location: URL_DESTINO'); . Let's suppose it is there for debug purposes and has no side effects.

Summing up:

<?php  
session_start();
$ingreso = new MvcController(); //Creamos un nuevo objeto
$ingreso->inicioSesionController(); //llamamos la funcion
?>

<center>
    <div class="admin">
        <h1 class="ingresoTitle">Inicio de Sesion del Administrador</h1>
        <form method="post">
                <input type="text" placeholder="Usuario" name="usuarioIngreso">
                <input type="password" placeholder="Contraseña" name="passwordIngreso">
                <input type="submit" value="Enviar">
        </form> 
    </div>  
</center>

And you remove the session_start from the method MvcController::inicioSesionController

Tip:

MVC is a design pattern (or software architecture pattern), not a model. If you say " Model MVC " it's like saying " Model Model View Controller ". You have to learn those things so that when someone from the commercial area arrives you can dizzy with terminology that you do not understand, but always taking care to quote the correct terminology. It is a tip to deal with bosses and clients.

To be more exact, MVC is NOT a design pattern, but an Architectural pattern.

    
answered by 23.11.2017 в 11:51