Unset a variable after clicking a link

Asked

Viewed 75 times

1

Help me guys wanted to know if it is possible to unset a variable through a click on a link through action or other solution?

  <a href="login.php" class="login" action=""><span class="navbarcolors"><?php if(isset($_SESSION['username']))
{
   echo 'logout'; 
}
else
{

   echo 'login';

}?>
  • Yes. Checks for Session. If so. Unset.

  • More if you want to do it in a variable or Session?

  • Check is already checking! Now I need to click on the link that unset the username variable because so when the variable exists echo logout and if click to logout make unset to echo login understand what I want to do?

  • want to unset $_SESSION[ username ] when link is pressed!

  • Unset($_SESSION["username"]; simple. Within the condition that checks if you are logged in.

  • But I want it to be unset when the link is clicked there is some possibility to do this?

  • Yes. In case. What is the login script? Go to the login page right? Get there you can check if you are logged in. If you have. You break the session. The correct thing was for you to have a link to a script just to log out.

  • Create a script logout.php. Break it there when it is triggered. There on your page make a condition to display the link. If you are logged in, show the login link. If you are not logged in, show the login link.

Show 3 more comments

1 answer

1

Unable to change a PHP variable/session via Javascript (without AJAX). What you can do is add a parameter to URL and check the user’s action in the file, for example.

<?php

    session_start();

    /**
     * Verifica se o parâmetro `action` existe na URL
     * Verifica se o valor do parâmetro `action` é "logout"
     * Verifica se a sessão existe
     *
     * Caso todas as condições acima sejam verdadeira, remove a sessão.
     */
    if (isset($_GET["action"]) && $_GET["action"] == "logout" && isset($_SESSION['username'])) {
        unset($_SESSION['username']);
    }

    /* Captura a ação */
    $action = !isset($_SESSION['username']) ? "login" : "logout";
?>

<a href="index.php?action=<?php echo $action ?>" class="login" action=""><span class="navbarcolors"><?php echo $action ?></span></a>

<?php
    /* ATENÇÃO! Adicionei a linha abaixo, SOMENTE por não saber como você está trabalhando com o formulário de login, ela é apenas para um teste. */
    $_SESSION['username'] = "ok";
?>

Demonstration:

Demonstração do código

  • Didn’t work!!

  • When I log in with the correct information the button shows login on it!

  • If possible test with this new modification

  • He is changing the login button to logout when the login is correct! But then I click on logout and the button stays the same!

  • Don’t get tired anymore tou noticing now a problem is that my href goes to the login.php page but when the user successfully logs in it sends to index.php and when he logs in he wanted it to go to index.php but it shows the button to say login!

  • Let’s take it one problem at a time. I added a demo to the answer. As I don’t know how you are using the login form, I can not elaborate a more precise code for your problem, but as you can see, the code is working correctly.

  • have teamviewer or something that can help me directly?

Show 2 more comments

Browser other questions tagged

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