473,406 Members | 2,549 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,406 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 5591
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

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

Similar topics

10
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...
2
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...
2
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...
0
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...
4
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...
3
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...
13
by: Goofy | last post by:
Does anyone know how I can kill a session by session ID ? -- Goofy
3
Nikky
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 ...
4
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.