如何使用按钮和javascript清除会话


How to clear the session using a button and javascript

Session是客户端人员,但是否可以使用javascript代码清除它?我也想有这样的想法,它能转换成jquery格式吗?非常感谢。

js

    $(function(){
  $("#closeTab").click(function() {
    window.parent.$('#tt').tabs('close','Create List');
        $.post("clear.php",function(data){
      });
  });
});

php

    <?
if (isset($_SESSION['lname']))
unset($_SESSION['lname']);
if (isset($_POST['creminder']))
unset($_SESSION['creminder']);
?>

这个可以吗?

对服务器进行ajax调用,并让服务器页面终止/结束会话

HTML

<a href="#" id="aKill" > Kill Session</a>

脚本

$(function(){
  $("#aKill").click(function(){
        $.post("serverpage.php",function(data){
        // if you want you can show some message to user here
     });
});

在serverpage.php中,执行php脚本以终止会话。

创建一个单独的文件以仅清除会话。clearsession.php

session_start();
session_destroy();

现在,提出一个简单的请求

$("#aKill").click(function(){
    $.get("clearsession.php");
}

下面或多或少介绍了一些基础知识。

功能:

function destroySession(){
  var theForm = $("#yourForm");
  //we don't need any ajax frame.
  theForm.each(function(){ this.reset() });
  $.ajax({
    url: 'destroysession.php',
    type: 'post',
    data: 'sure=1', //send a value to make sure we want to destroy it.
    success: function(data);
      alert(data);
    }
  });
}

PHP(destroysession.PHP)

<?php
  //whatever logic is necessary

  if(!empty($_POST['sure'])){ 
     $sure = $_POST['sure'];
     if($sure == 1){
       //logic to destroy session
       echo 'Session Destroyed!';
     }else if($sure != 1){
       //logic to perform if we're being injected.
       echo 'That value is incorrect. What are you doing over there?';
     }
  }else{
     //logic to perform if we're being injected.
     echo 'That value is incorrect. What are you doing over there?';
  }
?>

HTML:

<input type='button' value='reset' onclick='destroySession()'>