Function / Method to change a variable from another file in PHP?

1

I have a login page that takes a variable from another file, which at the beginning is empty, and places it below the login screen. If the user misses login / password or does not enter anything, the variable should be changed to "Invalid Login" on a second page that will redirect back to the login screen, where the text will appear in red.

I have, in the file index.php (login page):

<br/><p class='login-status'>
    <?php
        include "Controller/LoginStatus.php";
            echo $loginStatus;
    ?>
</p>

When the user clicks on the form login button, the information goes by post to the Login.php file:

include "../Model/Usuario.php";

$tipo = $_POST['tipo'];
$login = $_POST['login'];
$senha = $_POST['senha'];

$data = array($tipo, $login, $senha);
$error = false;
foreach($data as $field) {
    if (empty($_POST[$field])) {
      $error = true;
    }
}

if ($error) {
    include "LoginStatus.php";
    setStatus("Login inválido.");
    header("Location: ../");
} else {
    $user = new Usuario();
    $retorno = $user->verificarLogin($tipo, $login, $senha);

    if($retorno){
        echo "Login efetuado. Redirecionando...";
        header("Location: ../View");
    } else{
        include "LoginStatus.php";
        header("Location: ../");
    }
}

And in the file LoginStatus.php :

$loginStatus = "";
function setStatus($input){
    $loginStatus = $input;
}

However, you are not changing the loginStatus variable. I wanted to know what the error was, or if you have some more direct / easy method to change it from Login.php. Thank you in advance!

    
asked by anonymous 14.11.2015 / 03:57

1 answer

2

Do you save the status if the user is logged in or not?

The best way is to use a session for this:

You would no longer need the file LoginStatus.php

on your index.php

<?php session_start(); ?>
<br/><p class='login-status'>
    <?php
        if(isset($_SESSION['loginStatus'])){
            echo $_SESSION['loginStatus'];
        }
    ?>
</p>

and Login.php

session_start();
include "../Model/Usuario.php";

$tipo = $_POST['tipo'];
$login = $_POST['login'];
$senha = $_POST['senha'];

$data = array($tipo, $login, $senha);
$error = false;
foreach($data as $field) {
    if (empty($_POST[$field])) {
      $error = true;
    }
}

if ($error) {
    $_SESSION['loginStatus'] = "Os campos são obrigatórios.";
    header("Location: ../");
} else {
    $user = new Usuario();
    $retorno = $user->verificarLogin($tipo, $login, $senha);

    if($retorno){
        echo "Login efetuado. Redirecionando...";
        header("Location: ../View");
    } else{
        $_SESSION['loginStatus'] = "Login inválido.";
        header("Location: ../");
    }
}
    
14.11.2015 / 04:24