Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy a PHP session on clicking a link

Is this code valid?

<a href="#" onclick="<?php session_destroy();?>">Logout</a>
like image 874
mpsbhat Avatar asked Jul 10 '13 07:07

mpsbhat


People also ask

How do you destroy a session in PHP?

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.

How will you destroy the session?

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.

Does session end when browser closed PHP?

By default, session variables last until the user closes the browser.

How do you destroy a cookie session?

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.


2 Answers

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>
like image 124
user2067005 Avatar answered Oct 05 '22 23:10

user2067005


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();
like image 45
Goutam Pal Avatar answered Oct 06 '22 00:10

Goutam Pal