0

I have a login system in my website. I have these 3 files involved in the process. Config.php, functions.php and login.php

Config.php has the database connection functions.php has the functions required to retrieve and store data in the database.

When I try to login with any user credentials 1 of 3 things can happen:

  1. When I try to access with non-existent data, the message related to this is shown correctly
  2. When I try to access with wrong data, the message related to this is shown correctly
  3. When I try to access with correct data, I get the next errors in my error_log file:

PHP Warning: session_start(): Cannot send session cookie - headers already sent by (output started at .../functions.php on line 58

PHP Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at .../login.php:56) in .../functions.php on line 58

PHP Warning: Cannot modify header information - headers already sent by (output started at .../login.php:56) in .../login.php on line 67

I replaced the files path with ...

The same files have been working on a XAMPP server, this problem only appears when I want to run it on my host.

Here is my code:

functions.php

class LoginRegister
{
    function __construct($db)
    {
        $this->db = $db;
    }
    public function eventoLogin($usuario, $passw)
    {
        $query = $this->db->prepare("SELECT id,user FROM people WHERE user=? AND password=?");
        $query->execute(array(
            $usuario,
            $passw
        ));
        $userdata = $query->fetch();

        $num = $query->rowCount();
        if($num == 1) {
            session_start(); //<------------ LINE 58
            $_SESSION['login'] = true;
            $_SESSION['uid'] = $userdata['id'];
            $_SESSION['uname'] = $userdata['user'];
            $_SESSION['login_msg'] = "Sesión iniciada correctamente";
            return true;
        } else {
            return false;
        }
    }
}

login.php

<?php
    require_once "functions.php";
    $db = new DatabaseConnection();
    $user = new LoginRegister($db->pdo);
    if($user->getSession()){
        header('Location: actualizar.php');
        exit();
    }
?>
<html>
    <body>

        <?php //<---------- LINE 56
            if($_SERVER['REQUEST_METHOD'] == 'POST') {
                $usuario = $_POST['usuario'];
                $passw = $_POST['passw'];
            }

            if(empty($usuario) or empty($passw)) {
                echo "Falta el nombre de usuario y/o contraseña";
            } else {
                $login = $user->eventoLogin($usuario, $passw);
                if($login) {
                    header('Location: actualizar.php'); //<------- LINE 67
                }

                else {
                    echo "Error, nombre de usuario o contraseña incorrectos";
                }
            }
        ?>

        <div>
            <form action="" method="post" name="reg">
                <table>
                    <tr>
                        <td><input type="text" name="usuario" placeholder="Nombre de usuario"></td>
                    </tr>
                    <tr>
                        <td><input type="password" name="passw" placeholder="Contraseña"></td>
                    </tr>
                    <tr>
                        <td> <input type="submit" name="submit" value="Ingresar" onclick="return(submitreg());"></td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
He_slp13
  • 67
  • 5

0 Answers0