dqq9695
dqq9695
2018-05-12 13:56
浏览 75
已采纳

如何通过单击HTML按钮删除PHP会话?

I want to delete all sessions of my webpage using PHP when the user clicks on a specific button... Below is the related code:

<a href="#" class="log" name="log">delete sessions</a>                        
<?php                                                                         
//when button is clicked then run command: session_destroy()                                                                           
?>

If it isn't possible then is there a way I can destroy PHP sessions using javascript action listeners?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyan2267
    dongyan2267 2018-05-12 14:21
    已采纳

    Hi

    One of ways to achive it is simple form and destroy session when the specified variable (a flag) was sent via for example POST.

    The HTML form with hidden input and button:

    <form action="" method="POST">
        <input type="hidden" name="destroySession" value="1">
        <input type="submit" value="DESTROY SESSION" />
    </form>
    

    PHP code for catch the flag from form:

    $destroySessionFlag = filter_input(INPUT_POST, 'destroySession');
    if ($destroySessionFlag == 1) {
        session_destroy();
    }
    

    Cheers

    点赞 评论

相关推荐