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:
Yes. Checks for Session. If so. Unset.
– Willian Coqueiro
More if you want to do it in a variable or Session?
– Willian Coqueiro
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?
– AlmostDone
want to unset $_SESSION[ username ] when link is pressed!
– AlmostDone
Unset($_SESSION["username"]; simple. Within the condition that checks if you are logged in.
– Willian Coqueiro
But I want it to be unset when the link is clicked there is some possibility to do this?
– AlmostDone
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.
– Willian Coqueiro
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.
– Willian Coqueiro