Logout button php

前端 未结 2 1841
甜味超标
甜味超标 2021-02-01 10:18

I have this code and need the code to add a logout button, can anyone write out the code for a log out button that will log out the user, I read something about destroy session

相关标签:
2条回答
  • 2021-02-01 10:38

    When you want to destroy a session completely, you need to do more then just

    session_destroy();
    

    First, you should unset any session variables. Then you should destroy the session followed by closing the write of the session. This can be done by the following:

    <?php
    session_start();
    unset($_SESSION);
    session_destroy();
    session_write_close();
    header('Location: /');
    die;
    ?>
    

    The reason you want have a separate script for a logout is so that you do not accidently execute it on the page. So make a link to your logout script, then the header will redirect to the root of your site.

    Edit:

    You need to remove the () from your exit code near the top of your script. it should just be

    exit;
    
    0 讨论(0)
  • 2021-02-01 10:50

    Instead of a button, put a link and navigate it to another page

    <a href="logout.php">Logout</a>
    

    Then in logout.php page, use

    session_start();
    session_destroy();
    header('Location: login.php');
    exit;
    
    0 讨论(0)
提交回复
热议问题