Jin Yong
Jin Yong

Reputation: 43778

unset session in Javascript

Does anyone know is it possible for me to reset/remove the session for php in javascript?

Upvotes: 1

Views: 10634

Answers (5)

Youssef Subehi
Youssef Subehi

Reputation: 2870

you can do that by posting to another page to unset sessions

$.ajax("/unset_data.php", {"cache":false});

Upvotes: 0

Daniel Pryden
Daniel Pryden

Reputation: 60997

PHP will store session information on the server side, but use an HTTP cookie (that the browser is responsible for sending back on each request) as a "handle" to the server-side state. So if all you want to do is clear the session completely (so that on the next request PHP will start a new session), you can use the document.cookie object in JavaScript to manipulate the cookie directly.

Peter-Paul Koch's eraseCookie() function is probably the easiest way to do this.

Upvotes: 0

Noah Goodrich
Noah Goodrich

Reputation: 25263

Session management is all specific to the server side environment. In order to manipulate the server-side session, you will need to issue a request to the server. If you need to do this asynchronously (via javascript) then you can always use an AJAX request that will allow for asynchronous communication between the client-side environment (the user's browser) and the server-side environment.

Upvotes: 0

Sampson
Sampson

Reputation: 268434

Keep in mind that Javascript runs on the client, after the page has been downloaded. Session data exists only on the server. As such, Javascript (on the client) cannot touch session data (on the server). You'll have to communicate with a server-side PHP script to handle session-data. You can make asynchronous calls via Javascript to the PHP scripts. This would be the only route.

Example using jQuery

$("a.signOut").click(function(){
  $.post("signout.php", {}, function(){
    alert("You've been logged out.");
  });
});

Upvotes: 2

jldupont
jldupont

Reputation: 96836

There isn't the concept of "session" on the Client/JS side - it is a construct/state of the server side.

Of course you could be sending an indication back to the server if you wish: you can use AJAX to do that.

Upvotes: 2

Related Questions