473,225 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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 5582
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
384 256MB
Any easy tutorials on AJAX you'd recommend, never used i before??
Jan 25 '09 #3
5,390 Expert Mod 4TB
here you will find a simple example and some explaination

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

kind regards
Jan 25 '09 #6
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
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
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
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. }
Jan 25 '09 #10
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;
  6.     xmlHttp = new XMLHttpRequest;
  8.     xmlHttp.onreadystatechange = function() {
  9.         if (xmlHttp.readyState == 4) {
  10.             alert(xmlHttp.responseText);
  11.         }
  12.     }
  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>
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
384 256MB
This worked! How do i get it to work without the alert box?
Jan 25 '09 #12
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
384 256MB
Ah yes, it's working now in firefox but not in IE??
Jan 25 '09 #14
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
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
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
384 256MB
Works perfectly, thanks again!
Jan 25 '09 #18
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

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

Similar topics

by: Fred | last post by:
There is a setting in INIT.ORA that has the unintended side-effect of making sure the ALTER SYSTEM KILL SESSION command has immediate affect. Without this setting, I've seen some instances where...
by: yabba | last post by:
w2k server as webadmin i have occasion to kill a user session and couse them to login again. I have the sessionID saved in a file. is there a way to kill the user session without disturbing...
by: Fabrice | last post by:
Hello, I 'would like to build a system (based on database, not SqlServer but MySQL) to permit only one session per user. I'm using a form athentication. My Solution : --------------- When...
by: HM | last post by:
Hello ! To kill a session i used KILL -INT <PID>. This command wasn't successfull. The processus is still here when an 'ps ax' or a 'select * from pg_stat_activity' Is there an other way to...
by: drodrig | last post by:
Hi. I am trying to close/kill all processes that show visible windows on Windows XP. So far I've created a script that uses win32gui.EnumWindows to iterate through all windows, check for which...
by: Mangler | last post by:
I have 2 sessions that get created. One when the user logs in called "uname" and another called "idrma" when that user choses to begin a repair request. I know <%session.abandon% will kill both...
by: Goofy | last post by:
Does anyone know how I can kill a session by session ID ? -- Goofy
by: Nikky | last post by:
I m sending a session from 1 form to another form i want to kill that session when i came back to first form how can i do this. i m using more than one session at a time but i want to kill ...
by: mritunjay82 | last post by:
If any user is deleted from admin section and is still logged in he can keep on doing things on my website that need to be logged in. I want to kill this users session. The only possible way I...
by: spectrumdt | last post by:
Hello. I am running Fedora Linux and KDE, using the Konsole command line. When coding Python, I regularly make a bug causing my program to not terminate. But how do I kill the non-terminating...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.