Destroy a PHP session by clicking a link
It can be done. If you focus on using the onClick action, you can just use AJAX. First you will need to create ajax.php, which will 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 need to create a javascript file that tells ajax.php that you want to log out:
<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> Anyway, thanks for using StackOverflox and let me know how this happens, or if you need more help :)
TR