PHP + HTML Unexpected result with $_GET

Asked

Viewed 84 times

1

You are not receiving the false value, and displaying the Else block. It has been passed through the url and only executes the true value if block. Someone give me a help, I thank you. Follow 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>
  • 1

    The log is a string, have you tried to compare $logged in === "true" ? Or how it is passing in the url, value 1/0/true/false?

  • Not for $_GET, passing true or false, directly to the variable $logged in, sure, only I want to get for $_GET["logged in"] this value.

  • Okay, try to put it like this $logado = (boolean) $_GET['logado'];

  • I added (Boolean) and it didn’t work. Is there any other way to solve ? Using $_GET

  • 1

    both "true" and "false" are true when it comes to string. Try it the way @Rafaelwithoeft recommended: if ($logado === 'true') to make a literal comparison. Note that upper and lower case make a difference in this case.

  • @Thiago, what value is coming in the $logged in variable? or better in the url? What value is passed in the url?

  • The value is false, only it displays the content of if. Since it was to enter Else.

  • You tested if($logged in === 'true') ?

  • @Thiago You can’t paste the URL here for us to see?

  • Tested here now, solved, Thank you very much @Rafaelwithoeft

Show 5 more comments

1 answer

1


Try changing for that:

<!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 failure. You are using : and not { and } to define the block delimited by if.

Another thing is that your html is being closed before the tag h1 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 to a session and not passing it using the method GET, even if it is only for display of the welcome message / login to the user. Moreover the application URL gets cleaner.

  • The if: syntax is accepted. http://php.net/manual/en/control-structures.if.php#112231

  • Thanks @Rafaelwithoeft , I didn’t know this detail, I always used the keys, and I’ve never seen this syntax. I’ve updated the answer.

  • We’re always learning something, after all nobody knows everything :)

  • Richard, thanks for answering, I ran the altered code and still not showing the block after Else. With you the false value passed by the url presented the login screen ?

  • @Thiago, I changed the condition of if, I believe it will now resolve.

  • @Richarddias I changed here and solved, thank you very much.

Show 1 more comment

Browser other questions tagged

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