Convert a logIn to logOut after login (without changing page)

1

I have an html page and what I want is that when logging in on the same page, the logIn link is hidden and the logOut appears. How could I do it?

<div id='page-wrap'>
<header class='main' id='h1'>
<span class="right"><a href='../PHP/signUp.php'>SignUp</a> </span>
<span class="right" style="display:none;"><a href="../PHP/logout">LogOut</a> </span>
<span class="right" style="display:none;"><a href='../PHP/logIn.php'>LogIn</a> </span>

What I was doing before was logearme go to another page but what I want to do now is that the same page change.

@Francisco Romero what I need is that without changing the page I updated the link from logIn to logOut and for that I understand that I have to use AJAX. Regarding what I have done is this:

<?php 
    if($_SESSION["login"]){
        echo "<span class="right" style="display:none;"><a href="logout">LogOut</a> </span>";
        echo "<span> Anonimo </span>";
    }else{
        echo "<span class="right" style="display:none;"><a href='logIn.php'>LogIn</a> </span>";
        echo "<span> Erabiltzailea : <?php echo $_SESSION['login']; ?> </span>";
    }
?>
    
asked by UnaiLopez 13.11.2017 в 21:14
source

1 answer

2

One of the solutions you can do is create a session variable in your file logIn.php , eg: $_SESSION["logueado"] = true; and then make a condition that shows one or the other button depending on whether a user is logged in or not.

<?php 
   if($_SESSION["logueado"]){
?>
    <span class="right" style="display:none;"><a href="../PHP/logout">LogOut</a> </span>
<?php 
   }else{
?>
    <span class="right" style="display:none;"><a href='../PHP/logIn.php'>LogIn</a> </span>
<?php 
   }
?>

NOTE 1: In both files (both in which you create and in which you retrieve the session variable) you will have to use the session_start() statement at the start of each of the files to be able to use the session variables.

NOTE 2: In the file logout.php you would have to delete the session variable since you want to indicate that the user has left the page. For this, you could do it in the following way:

unset($_SESSION['logueado']);
    
answered by 13.11.2017 в 21:25