How to make the site execute a code sequence with a certain user?

0

Good morning, I have an administrative access page for the ADMs of my site but have a part of the code in one of the pages, to create and modify users and passwords, which I would like to appear only when the user "TESTES" is connected on the page. I'm a little lay in PHP so I think you've done something wrong or something is missing.

Within this page there is a STRING that calls the name of the connected user, and I am using this STRING to do that, which would be $_SESSION['nome_usuario'] .

<?php if ($_SESSION['nome_usuario'] = "TESTES") { ?>
   "Codigos em HTML e PHP que cria e modifica usuarios"...
<?php } ?>

In case you wanted SE user to be equal to "TESTES" it would display the codes and if it is not equal do not present anything. Thanks in advance for all the help.

    
asked by anonymous 15.10.2016 / 17:06

2 answers

0

Normally, when we develop a login system, we register the id of the user that is logged in, which would be the correct thing to do.

In this case, as you do the user comparison, being test user, you can solve this deadlock as follows:

<?php if ($_SESSION['nome_usuario'] == "TESTES") { ?>
   echo "Codigos em HTML e PHP que cria e modifica usuarios";
<?php } ?>

Use == to check equality, if you only use = , you will not be able to compare, but you will be setting / assigning the session as a test user.

The other way you can do that would be the right way:

At the moment you do the login and password verification in the database, you also define:

$_SESSION['id_usuario'] = $row['id_usuario'] // usuário do banco de dados

And in your user verification, you can check as follows:

<?php if ($_SESSION['nome_usuario'] == 1) { ?>
   echo "Codigos em HTML e PHP que cria e modifica usuarios";
<?php } ?>

In this case, id=1 , is the test id.

    
15.10.2016 / 17:10
0

You can do this check with this code by changing only the assignment operation = , for a comparison operator ==

<?php if ($_SESSION['nome_usuario'] == "TESTES") { ?>
   "Codigos em HTML e PHP que cria e modifica usuarios"...
<?php } ?>

We recommend you do this check:

  • When accessing this page that creates and modifies users, if it is not the test user, display an error message;
  • If there is a menu that goes to this page, the link to this page should also be hidden.
15.10.2016 / 17:20