3

I create a session in my php script.
I want to destroy my php session in javascript.
when I click on Destroy Session then javascript function destroy() call and destroy SESSION['user'].

 <?php
    ob_start();
session_start();
    SESSION['user'] = "test 123";
    echo "<a onClick = 'destroy()'>Destroy Session</a>";
 ?>

  <script>
      Function destroy(){
       session_destroy();  // Like `PHP` I want destroy Session in `javascript`
      }
  </script>
3
  • You can't access SESSION directly with javascript. You'll have to either POST or use AJAX. Commented Apr 17, 2014 at 7:02
  • PHP is ServerSide - so your sessions are serverside, too. Javascript is clientside. Why you won't refresh the site by javascript to an script, that destroys your session? Commented Apr 17, 2014 at 7:02
  • 1
    I think you cannot destroy php session in javascript. because javascript is client side and php is server side scripting. so if you want to achieve this, you can make it by making a ajax call at some point and the php file to whom your ajax is pointing will destroy your session. Commented Apr 17, 2014 at 7:03

3 Answers 3

5

I think you should use AJAX to destroy function from Javascript. Like :

.js code :

function destroy_session(){
    var xmlhttp = getXmlHttp();
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('GET','./destroy_session.php', true);
    xmlhttp.onreadystatechange=function(){
       if (xmlhttp.readyState == 4){
          if(xmlhttp.status == 200){
             alert(xmlhttp.responseText);
         }
       }
    };
    xmlhttp.send(null);
}

destroy_session.php code:

<?php
    session_start();
    $_SESSION = array();
    if (ini_get("session.use_cookies")) {
       $params = session_get_cookie_params();
       setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
       );
    }
    session_destroy();
    echo 'Session was destroyed';
?>
Sign up to request clarification or add additional context in comments.

Comments

2

you cant directly destroy the session of php within javascript, since javascript is running on the client and php is running on the server.

but you can erase the session cookie from php within php - when its used!

but this detaches only the client from the session, no destroying the session.

Comments

0

What you can do is use AJAX to load a PHP page that executes a session_destroy(). jQuery has very easy ajax functions, documented here.

5 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
... Is there like a 3-year-old queue of "answers that need review" somewhere or something? Beyond that, I disagree with the statement that this is "essentially a link only answer." I don't understand the purpose of reviewing a 3-year-old answer that is (1) not the top answer and (2) not the accepted answer. This just sounds like reputation farming.
Yes - there is a queue. No rep farming, however - it's just to help keep the site clean. If you want more information about this process, this post is helpful. Not everyone agreed with my assessment - and the link you provided is helpful. If the the accepted answer had included the jQuery link as well, i think your answer would have been removed.
Not trying to split hairs - if you had added a code example even after the accepted answer was accepted, I do not think you're post would have been flagged (as the link is helpful.)
... Well. That actually explains that. Thank you very much! I appreciate you explaining it to me. I'll definitely keep this in mind for future answers!

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.