0

I am trying to do a simple thing, create a login page that shows successful login and logoff messages, but it always shows the successful login message ("Logado com sucesso"). My hosting is Hostinger. Here is my code:

<?php
require_once('db.php');
session_start();
if(isset($_POST['action'])){
    if($_POST['action'] = 'login'){
        $_SESSION['login'] = $_POST['login'];
        $msg = 'Logado com sucesso!';
    } elseif($_POST['action'] = 'logoff') {
        $msg = 'Deslogado com sucesso!';
        unset($_SESSION['login']);
    }
}
?>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <div id="container">
        <form method="POST">
            <? if(!$_SESSION['login']){ ?>
                <h3>Preencha os seus dados abaixo</h3>
                <input type="hidden" name="action" value="login">
                <input type="text" required name="login" placeholder="Nome de usuário">
                <input type="password" required name="senha" placeholder="Senha">
                <input type="submit" value="Entrar">
            <? } else { ?>
                <h3>Você está logado como</h3>
                <input type="hidden" name="action" value="logoff">
                <input type="text" required name="login" disabled value="<?=$_SESSION['login']?>">
                <input type="submit" value="Sair">
            <? } ?>
            <? if(isset($msg)){ ?>
                <div id="msg"><? echo $msg?></div>
            <?} ?>
        </form>
    </div>
</body>
</html>

1 Answers1

3

Your if statements are performing assignment and not a comparison.

For example

if($_POST['action'] = 'login')

is assigning $_POST['action'] = 'login' and will always be TRUE.

You need == so that would become

if($_POST['action'] == 'login')
biesior
  • 55,576
  • 10
  • 125
  • 182
TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28