2012-03-20 39 views
1

會話是客戶端工作人員,但可以通過使用javascript代碼清除它嗎? 我想也有這樣的想法,它可以轉換爲jQuery格式?謝謝。如何使用按鈕和javascript清除會話

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']); 
?> 

這是一個好不好?

+0

清除或結束會話?有一個很大的區別... – Jon 2012-03-20 02:22:49

+0

我認爲應該取消設置($ _ SESSION ['cart']);或$ _SESSION ['cart'] ='' – user782104 2012-03-20 02:24:47

回答

5

,從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腳本來終止會話。

+0

我可以做到沒有警報嗎?你會介意稍微修改它嗎?謝謝 – user782104 2012-03-20 02:29:48

+2

@ user782104:然後把它拿出來。我現在更新了答案 – Shyju 2012-03-20 02:35:01

+0

非常感謝。我會嘗試報告,如果這項工作或不 – user782104 2012-03-20 02:42:57

1

以下內容或多或少涵蓋了基本知識。

的功能:

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()'> 
+0

thx爲您提供幫助 – user782104 2012-03-20 05:42:35

2

創建一個單獨的文件來僅清除會話。 clearsession.php

session_start(); 
session_destroy(); 

現在,做一個簡單的請求

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