PHP + HTML Unexpected result with $ _GET

1

You are not getting the value false, and displaying the else block. It was passed by the url and only executes the if block of true value. Someone gives me a help, thanks in advance. Here is the code below:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<?php $logado = $_GET["logado"]; ?>

<html lang="pt-br">
    <head>
        <meta charset="UTF-8">
        <title>IF Alternativo</title>
    </head>
    <body>
        <?php if($logado) : ?>

    </body>
</html>      <h1>Bem vindo ao sistema!</h1>


        <?php else : ?>

        <h1>Faça o login</h1>

        <form>
                <input type="text" placeholder="Login" name="login"/>
                <input type="password" placeholder="Senha" name="senha"/><br/>
                <input type="submit" value="Entrar">
        </form>

            <?php endif; ?>

    </body>
</html>
    
asked by anonymous 05.03.2015 / 20:31

1 answer

1

Try changing this:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<?php $logado = $_GET["logado"]; ?>

<html lang="pt-br">
    <head>
        <meta charset="UTF-8">
        <title>IF Alternativo</title>
    </head>
    <body>
        <?php if($logado == 'true') { ?>

            <h1>Bem vindo ao sistema!</h1>

        <?php } else { ?>

            <h1>Faça o login</h1>

            <form>
                    <input type="text" placeholder="Login" name="login"/>
                    <input type="password" placeholder="Senha" name="senha"/><br/>
                    <input type="submit" value="Entrar">
            </form>

        <?php } ?>

    </body>
</html>

[ Editing ) The syntax using : is supported by php , my fault. You are using : and not { and } to define the block delimited by if .

Another thing is also that your html is being closed before the h1 tag as you can see below:

  
</body> </html>      <h1>Bem vindo ao sistema!</h1>

    <?php else : ?>

Another thing you should also consider is storing the value of the $ variable logged in a session and not passing it using the GET method, even if it is just for displaying the welcome / login message to the user . In addition, the URL of the application gets cleaner.

    
05.03.2015 / 20:41