redirect to a PHP page with header

0

Good day.

I have a script which after a user signs in sends it to the control panel of their account through a link that has to be clicked, everything works great, but I think it would be better to automatically redirect to the control panel and so not having to click on the mentioned link, the problem is that it does not redirect with header, the following error comes out

  

Warning: Can not modify header information - headers already sent by   (output started at   /home2/gabriel/tecflucol.com/sistema/PHP/checklogin.php:4) in   /home2/softwareiii.com/sistema/PHP/checklogin.php on line 26

How could I solve it and redirect the page automatically?

Here's the code:

<?php
    session_start();
?>

<?php
    include('../PHP/conectar.php');

    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM 'usuarios' WHERE 'usuario'='$username';";
    $result = $conexion->query($sql);

    if ($result->num_rows > 0)
    {
        $row = $result->fetch_array(MYSQLI_ASSOC);
    }

    if (password_verify($password, $row['contrasena']))
    {
        $_SESSION['loggedin'] = true;
        $_SESSION['usuario'] = $username;
        $_SESSION['start'] = time();
        $_SESSION['expire'] = $_SESSION['start'] + (5 * 800000);

        //rediriguir automanticamente
        header('Location: panel_de_control.php');

        //estas dos lineas funcionan bien pero la idea es suprimirlas
        //echo "Bienvenido(a)! ".$_SESSION['usuario'];
        //echo "<br><br><a href=panel_de_control.php>PANEL DE CONTROL</a>";
    }
    else
    {
      echo "Usuario o contraseña estan incorrectos.";
      echo "<br><a href='../login.html'>Volver a Intentarlo</a>";
    }
     mysqli_close($conexion);
?>
    
asked by Gabriel Uribe Gomez 01.06.2018 в 17:11
source

1 answer

1

PHP%% error% usually occurs when the information is output before modifying the header with Cannot modify header information

In the line 4 of your checklogin.php you have a blank space , that space is generating that information output, if for example, you put a header('Location: panel_de_control.php'); , before your echo("Hola") would possibly result in the same error.

The solution is to eliminate header() in line 3 and ?> in line 5, leaving only the <?php at the beginning and end.

    
answered by 01.06.2018 / 17:37
source