Cannot modify header information

Hi. I am relatively new to PHP.
I’m trying to make a user authentication, and sending the user to another page if succeeded, with the following code:

<?php
include ('../conexion.php');

$nombreUsuario = $_POST['nombreusuario'];
$password = $_POST['password'];

var_dump($nombreUsuario);
var_dump($password);

$consultaAdmin= "SELECT * FROM usuarios WHERE nombre_usuario = '$nombreUsuario' AND password = '$password' AND tipo = 'admin'";
$resultadoAdmin = mysqli_query($conn , $consultaAdmin);
$cantidad= mysqli_num_rows($resultadoAdmin);

if($cantidad==1) {
	header("location:listado_recetas.php?bienvenida=si");
}else {
	header("location:index.php?error=si");
}
?>

I am getting this error:

string(8) "brsastre" string(10) "igor123123" 
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/recetario/admin/validar.php:7)...

If I remove var_dump’s it works just fine.

Why is this happening?

The var_dumps are sending output to the browser.
With the output, headers get sent along with it.
header() must be before anything is output to the browser.

If you are just debugging something on a local machine, I think you can use ob_start and ob_flush. Then if you are uploading it to a live server, you shouldn’t be displaying any debugging to the user. So you should remove var_dump, ob_start, and ob_flush if it’s a live server.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.