By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,450 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,450 IT Pros & Developers. It's quick & easy.

Can you kill a PHP session through JS?

100+
P: 384
I have this function being called when a user clicks on a logout link, but i'm unsure if i can kill the current session by calling session_destory(); from JS??
Expand|Select|Wrap|Line Numbers
  1. function logout() {
  2.     //kill session here ex. session_destory();
  3. }
Jan 25 '09 #1
Share this Question
Share on Google+
18 Replies


gits
Expert Mod 5K+
P: 5,390
you may use an ajax-call to call a php-script that does what you want ...

kind regards
Jan 25 '09 #2

100+
P: 384
Any easy tutorials on AJAX you'd recommend, never used i before??
Jan 25 '09 #3

gits
Expert Mod 5K+
P: 5,390
here you will find a simple example and some explaination

kind regards
Jan 25 '09 #4

100+
P: 384
Ok, I've got this far now, I'm not sure how to set it so it works on a click of a link?
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function logout()
  3. {
  4. var xmlHttp;
  5. try
  6.   {
  7.   // Firefox, Opera 8.0+, Safari
  8.   xmlHttp=new XMLHttpRequest();
  9.   }
  10. catch (e)
  11.   {
  12.   // Internet Explorer
  13.   try
  14.     {
  15.     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  16.     }
  17.   catch (e)
  18.     {
  19.     try
  20.       {
  21.       xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  22.       }
  23.     catch (e)
  24.       {
  25.       alert("Your browser does not support AJAX!");
  26.       return false;
  27.       }
  28.     }
  29.   }
  30.   xmlHttp.onreadystatechange=function()
  31.     {
  32.     if(xmlHttp.readyState==4)
  33.       {
  34.       document.logout.value=xmlHttp.responseText;
  35.       }
  36.     }
  37.   xmlHttp.open("GET","logout.php",true);
  38.   xmlHttp.send(null);
  39.  }
  40. </script>
Expand|Select|Wrap|Line Numbers
  1. <a href="#" name="logout" onclick="logout(); return false;">Logout</a>
Jan 25 '09 #5

gits
Expert Mod 5K+
P: 5,390
doesn't it work ... from a first quick look it should do?

kind regards
Jan 25 '09 #6

100+
P: 384
I've gone over all the code and i've put a pop up message in the logout.php file but it's not showing the pop up when i click logout so it's never getting to the logout.php file?
Jan 25 '09 #7

gits
Expert Mod 5K+
P: 5,390
the logout.php is called async ... and you will just get a notification in the onreadystatechange-handler of the request, so just echo something in the php-script and alert the responseText, to see that the script is called ...
Jan 25 '09 #8

100+
P: 384
I tried this but nothing:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. xmlHttp.onreadystatechange=function()
  3.     {
  4.         if(xmlHttp.readyState==4)
  5.         {
  6.             document.logout.value=xmlHttp.responseText;
  7.             alert(xmlHttp.responseText);
  8.         }
  9.     }
  10. ...
Jan 25 '09 #9

gits
Expert Mod 5K+
P: 5,390
try the following please:

Expand|Select|Wrap|Line Numbers
  1. xmlHttp.onreadystatechange = function() {
  2.     if (xmlHttp.readyState == 4) {
  3.         alert(xmlHttp.responseText);
  4.     }
  5. }
  6.  
Jan 25 '09 #10

gits
Expert Mod 5K+
P: 5,390
simplified example for firefox:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <script type="text/javascript">
  3. function logout() {
  4.     var xmlHttp;
  5.  
  6.     xmlHttp = new XMLHttpRequest;
  7.  
  8.     xmlHttp.onreadystatechange = function() {
  9.         if (xmlHttp.readyState == 4) {
  10.             alert(xmlHttp.responseText);
  11.         }
  12.     }
  13.  
  14.     xmlHttp.open("GET", "foo.php", true);
  15.     xmlHttp.send(null);
  16. }
  17. </script>
  18. <body>
  19.     <a href="#" onclick="logout(); return false;">foo</a>
  20. </body>
  21. </html>
  22.  
and foo.php should just echo/print something. then you should get the printed text in the alert box. do you get any errors?
Jan 25 '09 #11

100+
P: 384
@gits
This worked! How do i get it to work without the alert box?
Jan 25 '09 #12

gits
Expert Mod 5K+
P: 5,390
yes ... the php-script is called and executed ... and you should know whether it does what it should ... in case you wrote it for yourself? ;)

kind regards
Jan 25 '09 #13

100+
P: 384
Ah yes, it's working now in firefox but not in IE??
Jan 25 '09 #14

gits
Expert Mod 5K+
P: 5,390
in case you use your above shown code tho create an instance of the XMLHttpRequest-object it should work. do you get any errors?
Jan 25 '09 #15

100+
P: 384
I got it working in IE was something to do with the cache in IE so it was working the whole time.

One last question, how would you go about adding a redirect on the successful click of logout, would you add it in the JS or the logout.php file?

Thanks again for all your help.
Jan 25 '09 #16

gits
Expert Mod 5K+
P: 5,390
you may use:

Expand|Select|Wrap|Line Numbers
  1. window.location.href = 'whatever_url';
in the onreadystatechange-handler in the JavaScript-code. where the url might be the responseText in case you need that.

kind regards
Jan 25 '09 #17

100+
P: 384
Works perfectly, thanks again!
Jan 25 '09 #18

gits
Expert Mod 5K+
P: 5,390
no problem ... ;) ... you are welcome. post back to the forum anytime you have more questions ...

kind regards
Jan 25 '09 #19

Post your reply

Sign in to post your reply or Sign up for a free account.