Is this code valid?
<a href="#" onclick="<?php session_destroy();?>">Logout</a>
A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.
In order to kill the session altogether, the session ID must also be unset. If a cookie is used to propagate the session ID (default behavior), then the session cookie must be deleted.
By default, session variables last until the user closes the browser.
If you want to unset all of the values, you can just run a session_destroy() . It will unset all the values and destroy the session. But the $_SESSION array will still work in the same page after session_destroy() . Then you can simply run $_SESSION = array() to reset it's contents.
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>
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With