Destroy a PHP session on clicking a link
Asked Answered
R

7

9

Is this code valid?

<a href="#" onclick="<?php session_destroy();?>">Logout</a>
Rafferty answered 10/7, 2013 at 7:29 Comment(0)
M
16

No it is not a valid code. It will destroy the session at the time of loading the php page.

For destroying session on click you should write

<a href="logout.php" >Logout</a>

in logout.php

session_destroy();
Mulder answered 10/7, 2013 at 7:32 Comment(1)
The session should be started before using session_destroy().Fingerling
N
16

Make a page called logout.php

Logout.php_____

<?php
Session_start();
Session_destroy();
header('Location: ' . $_SERVER['HTTP_REFERER']);

?>

Your page______

<a href="Logout.php">Logout</a>
Nunn answered 10/7, 2013 at 7:33 Comment(1)
This is the correct way to do it. One should start the session before destroying it.Fingerling
C
7

Wrong code. you can use this code:

<?php if($_GET['logout']==1) session_destroy(); ?>
<a href="?logout=1">Logout</a>
Corpora answered 10/7, 2013 at 7:32 Comment(0)
A
4

That code will already destroy the session before clicking the link, you should do it like this:

HTML Page:

<a href="sessiondestroy.php">Logout</a>

Sessiondestroy.php :

<?=session_start(); session_destroy(); ?>
Angelia answered 10/7, 2013 at 7:33 Comment(0)
K
3

no its not valid...onclick is a client side event. you can do this instead.

     <a href="logout.php">logout</a>

and create a file called logout.php and include the session_destroy(); statement

       <?php
           session_destroy();

           //do other things... like redirect to a deafault/login page

         ?>
Kimsey answered 10/7, 2013 at 7:37 Comment(0)
L
2

No, its not logical to call server-side function from client-side, onClick is an event occurs at client side, so, it cant call session_destroy()because it's server-side (PHP Function) which is not available at client side

Lengthen answered 10/7, 2013 at 7:31 Comment(0)
G
2

It's possible to do that. If you are focused on using the onClick action, you could simply use AJAX. First you would have to create ajax.php, which would look like this:

<?php
//AJAX dynamic callback
if(isset($_GET['action'])){
    if($_GET['action'] == 'logout'){
        //destroy the session
        session_destroy();
        echo 'Logout success!';
        //redirect the user to a default web page using header
        header("location:http://example.com/");
    }
}
?>

Then you would want to create a javascript file that would tell ajax.php that you wanted to logout:

<script>
function logout()
 {
 var xmlhttp;
 if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
 else
   {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 xmlhttp.onreadystatechange=function()
   {
   if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
     document.getElementById("Logoutbutton").innerHTML=xmlhttp.responseText;
     }
   }
 xmlhttp.open("GET","ajax.php?action=logout",true);
 xmlhttp.send();
 }
</script>

Anyways, thanks for using StackOverflox, and please report back how it goes, or if you need additional help :)

TP

Garfield answered 10/7, 2013 at 7:48 Comment(1)
Most complicated logout I have ever, and I mean ever seen.Nunn

© 2022 - 2024 — McMap. All rights reserved.