How to change login and logout links dynamically in HTML page in PHP?

后端 未结 4 1594
梦谈多话
梦谈多话 2021-01-01 06:11

I have the following HTML code. I have 2 php files: login.php and logout.php. When any user first comes to my page, I would like him to see the login link. After clicking lo

相关标签:
4条回答
  • 2021-01-01 06:47

    I am assuming you are using a session to check whether user logged in or not. If not, create a session variable and set once logged in. Clear the session variable on log out. So you can check

    if(isset($_SESSION['logged_in'])){
    //your logout link
    }else{
    //your login link
    }
    
    0 讨论(0)
  • 2021-01-01 07:02
     <?php 
            if ($signout) 
            { 
                echo "<li><a href=logout.php >Logout</a></li>";
            } 
            else 
            { 
                echo "<li><a href=register.php >Register</a></li>";
                echo "<li><a href=login.php >Login</a></li>";
            } 
    ?>
    
    0 讨论(0)
  • 2021-01-01 07:04

    Try this.

    You need to set a variable $logged_user to identify whether the user is logged in or not

    <header class="group">
    <nav class="group">         
        <ul>
            <?php if ($logged_user) { ?>
                <li><a href="logout.php" title="">Logout</a></li>             
            <?php } else { ?>
                <li><a href="register.php" title="">Register</a></li>
                <li><a href="login.php" title="">Login</a></li>   
    
            <?php } ?>
        </ul>
    </nav>
    

    0 讨论(0)
  • 2021-01-01 07:08
    <?php 
        if(isset($_SESSION['user_id'])) { 
            echo "<form method='POST' action='".userLogout()."'>";  ?>
                <li><button type="submit" name="userlogout" id="logout-button" class="button-logout">LOGOUT</button></li>
    
    <?php } else { ?>
            <li><a href="login">LOGIN</a></li>
        </form>
    <?php }
    ?>
    
    0 讨论(0)
提交回复
热议问题