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

Asked

Viewed 865 times

1

I have a login page that takes a variable from another file, which at the beginning is empty, and puts below the login screen. If the user misses login/password or does not enter anything, the variable must 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 login button of the form, the information goes by post to the file Login.php:

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 archive LoginStatus.php:

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

However, it is 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.!

1 answer

2


You who save the status if the user is logged in or not?

The best way is to use a session for that:

You wouldn’t need the file anymore LoginStatus.php

in his index.php

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

and the 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: ../");
    }
}
  • Actually I want that if the user misses the login or leaves blank, when redirecting back to the login page be changed the variable, showing "Invalid login" below.

  • This is what the script does

  • Okay, it worked. Thank you so much :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.