1

Session is client side staff, but is it possible through clear it using javascript code? I would like too have the idea like this and ,can it convert to jquery format? Thank you.

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

Is this one ok ?

2
  • Clear or end the session? There's a big difference... Commented Mar 20, 2012 at 2:22
  • I think should be unset($_SESSION['cart']); or $_SESSION['cart']='' Commented Mar 20, 2012 at 2:24

3 Answers 3

5

make an ajax call to the server and let your server page kills/ends the session

HTML

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

Script

$(function(){

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

and in your serverpage.php, Execute the PHP script to terminate the session.

Sign up to request clarification or add additional context in comments.

6 Comments

Can i do it without alert? would you mind slightly modify it? thank you
@user782104 : Just take it out the alert then . I updated the answer now
no ,of course, would you help me , how to go to a specific page when pressing a button?
@user782104 : Which page ? elaborate that pls. May be as a new question
I elaborate it clearly, take a look if you are free. stackoverflow.com/questions/9781152/…
|
2

Create a separate file to clear the session only. clearsession.php

session_start();
session_destroy();

Now, make a simple request

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

Comments

1

The below covers the basics more or less.

The Function:

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);
    }
  });
}

The 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?';
  }
?>

The HTML:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.