469,323 Members | 1,517 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Can you kill a PHP session through JS?

384 256MB
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
18 5295
gits
5,390 Expert Mod 4TB
you may use an ajax-call to call a php-script that does what you want ...

kind regards
Jan 25 '09 #2
ziycon
384 256MB
Any easy tutorials on AJAX you'd recommend, never used i before??
Jan 25 '09 #3
gits
5,390 Expert Mod 4TB
here you will find a simple example and some explaination

kind regards
Jan 25 '09 #4
ziycon
384 256MB
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
5,390 Expert Mod 4TB
doesn't it work ... from a first quick look it should do?

kind regards
Jan 25 '09 #6
ziycon
384 256MB
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
5,390 Expert Mod 4TB
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
ziycon
384 256MB
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
5,390 Expert Mod 4TB
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
5,390 Expert Mod 4TB
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
ziycon
384 256MB
@gits
This worked! How do i get it to work without the alert box?
Jan 25 '09 #12
gits
5,390 Expert Mod 4TB
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
ziycon
384 256MB
Ah yes, it's working now in firefox but not in IE??
Jan 25 '09 #14
gits
5,390 Expert Mod 4TB
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
ziycon
384 256MB
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
5,390 Expert Mod 4TB
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
ziycon
384 256MB
Works perfectly, thanks again!
Jan 25 '09 #18
gits
5,390 Expert Mod 4TB
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.

Similar topics

10 posts views Thread by Fred | last post: by
2 posts views Thread by yabba | last post: by
reply views Thread by HM | last post: by
4 posts views Thread by drodrig | last post: by
13 posts views Thread by Goofy | last post: by
Nikky
3 posts views Thread by Nikky | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.