473,800 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

logout in php

108 New Member
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.Tha nk s
Jan 10 '10 #1
1 23216
Dormilich
8,658 Recognized Expert Moderator Expert
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
3484
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 the link, if the user is logged. So far, so good. It's working. Aspx
0
1955
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 .MDB file. Same with the logout but i need that logout time should get stored in the row where login details get stored and "LoginTime - LogoutTime = Total Time" calulates and stored in last field name TotalTime. Also how to check whether user...
25
3335
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, when I press back button, I get to the page that says "Page has Expired" but Firefox does not do this. I think it's something to do with sessions not properly unset or something like that but I haven't been able to figure it out. I am
12
4589
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 am tring to go back threw back button of Browser....it send me last visted pages(means sessons not expire properly). How can i solve it... One more thing is that the script is working properly on localhost....problem occures when i uploaded it on...
10
4077
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()" "MM_nbGroup('down','group1','logout','',1)""MM_nbGroup('down','group1','logout','../button_images/logout_button_down.png',1)" onMouseOver="MM_nbGroup('over','logout','../button_images/logout_button_down.png','../button_images/logout_button_down.png',1)" onMouseOut="MM_nbGroup('out')" ><img name="logout"...
1
14061
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. following are entries in struts-config.xml file <action input="index.jsp" name="loginform" path="/login" scope="session" type="com.myapp.struts.loginaction"> <forward name="success" path="/main.jsp"/>
3
5000
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 user gets authenticated using the login control and the build-in database, but there are times when I want to logout the user and send them back to the login page. At this point the "isAuthenticated" method should return false.
1
3328
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 for the hidden textbox on the logout sub.. but how do i get round this? can i not change the value onclick? <div id=Rightbody> <!--<form id="RightBodyForm" name="RightbodyForm" method="post" action="Guestbook2.asp">--> <% Response.Expires...
4
3427
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(); Response.Clear(); FormsAuthentication.SignOut(); Response.Redirect("login.aspx"); }
10
4823
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 Jones, then login as David Smith the Welcome message below will show "Welcome Davey". And it will be Davey's information that is accessible - not David Smith's. So something is amiss but I don't know what. (BTW, this login script is based on the...
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10253
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.