473,396 Members | 1,826 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,396 software developers and data experts.

logout in php

108 100+
Hi,I have created a script where I want to use onclick event and javascript to log out.Here r my coodes ..
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.                     function logout(){
  3.                 <?php session_destroy();?>
  4.             }
  5. </script>
  6.  
  7. <body>
  8. <a href="#" onclick="logout()">logout</a>
  9. </body>
Whenever,I reload the page,the sessions variables are destroyed without pressing the logout link...
I want know how this is achieved and how can I logout from a session..

Any help will be kindly appreciated.Thank s
Jan 10 '10 #1

✓ answered by Dormilich

that is because the PHP (that is, the session_destroy() function) is executed before the complete HTML is sent to the browser and JavaScript can be executed. (also note the output of your PHP code in the HTML source code – there shouldn’t be any)

to solve this there are several possibilities
1) make an AJAX call to a PHP script file, that executes session_destroy()
Expand|Select|Wrap|Line Numbers
  1. // JavaScript, pseudo code
  2. function logout()
  3. {
  4.     // simply calling the logout.php page
  5.     Ajax.post("logout.php");
  6. }
  7. // PHP
  8. <?php # file: logout.php
  9.     session_start();
  10.     session_destroy();
  11. ?>
2) submit a form (you only need the submit button) to this page and if the button was pressed, destroy the session.
Expand|Select|Wrap|Line Numbers
  1. // HTML/PHP
  2. <form action="" method="post">
  3.     <input type="submit" name="logout" value="yes">
  4.     <?php
  5.         if (isset($_POST["logout"]))
  6.         {
  7.             session_destroy();
  8.         }
  9.     ?>
  10. </form>
3) reload the page via JavaScript, passing an URL parameter indicating the session to be killed.
Expand|Select|Wrap|Line Numbers
  1. // JavaScript
  2. function logout()
  3. {
  4.     window.location.href = "page.php?logout=yes";
  5.     <?php
  6.         if (isset($_GET["logout"]))
  7.         {
  8.             session_destroy();
  9.         }
  10.     ?>
  11. }

1 23027
Dormilich
8,658 Expert Mod 8TB
that is because the PHP (that is, the session_destroy() function) is executed before the complete HTML is sent to the browser and JavaScript can be executed. (also note the output of your PHP code in the HTML source code – there shouldn’t be any)

to solve this there are several possibilities
1) make an AJAX call to a PHP script file, that executes session_destroy()
Expand|Select|Wrap|Line Numbers
  1. // JavaScript, pseudo code
  2. function logout()
  3. {
  4.     // simply calling the logout.php page
  5.     Ajax.post("logout.php");
  6. }
  7. // PHP
  8. <?php # file: logout.php
  9.     session_start();
  10.     session_destroy();
  11. ?>
2) submit a form (you only need the submit button) to this page and if the button was pressed, destroy the session.
Expand|Select|Wrap|Line Numbers
  1. // HTML/PHP
  2. <form action="" method="post">
  3.     <input type="submit" name="logout" value="yes">
  4.     <?php
  5.         if (isset($_POST["logout"]))
  6.         {
  7.             session_destroy();
  8.         }
  9.     ?>
  10. </form>
3) reload the page via JavaScript, passing an URL parameter indicating the session to be killed.
Expand|Select|Wrap|Line Numbers
  1. // JavaScript
  2. function logout()
  3. {
  4.     window.location.href = "page.php?logout=yes";
  5.     <?php
  6.         if (isset($_GET["logout"]))
  7.         {
  8.             session_destroy();
  9.         }
  10.     ?>
  11. }
Jan 10 '10 #2

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

Similar topics

2
by: Jon Natwick | last post by:
I'm trying to add a dynamically created logout link on my pages. The link to the logout page will show if the user is logged in. I put a placeholder on the aspx page and then dynamically create...
0
by: ShailShin | last post by:
Hi All, Developing an VB App for Login and logout tracking. In App Form there are two buttons login and logout. When user click on login the loginName, LoginTime and LoginDate get stored in...
25
by: crescent_au | last post by:
Hi all, I've written a login/logout code. It does what it's supposed to do but the problem is when I logout and press browser's back button (in Firefox), I get to the last login page. In IE,...
12
kamill
by: kamill | last post by:
I have done a logout page for logout from admin section and provides a link to logout from admin section.Whenever i clicked on logout link it redirected to index.php of admin section......BUT when i...
10
by: chaos | last post by:
How to do logout alert message when i press on the logout image <a href="../logout.php" target="_top" onClick="return logout()"...
1
by: shrik | last post by:
hi everybody. I have following problem. There are two pages. index.jsp and main.jsp in my application Index.jsp contains logging interface in . It submits password and userid to loginform bean. ...
3
by: kpg* | last post by:
Hi all, I want to perform the same action the the loginstatus control does to logout a user programatically, but I can't seem to find a 'logout' method in any of the membership classes. The...
1
by: Kandiman | last post by:
Hiya, i made a asp page, and one of my divs (as a include) is as below. the problem is if the main page is resubmitted, i get logged out again?... heres the code.. i think its on the value=true...
4
by: shahidrasul | last post by:
hi in my project when i click on logout anchor it goes to logout page and my code in logout page is if (Session != null) { Session = null; Session.Abandon(); ...
10
by: DavidPr | last post by:
When I logout as one user and log in under a different user, it opens with the last user's information. User 1 - Unsername: Davey Jones User 2 - Unsername: David Smith I log out from Davey...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.