I am using the following code to invalidate the session. I have linked to logout.php
in many pages. If that logout link is clicked the logout.php
page is called. The following is the code in logout.php
.
unset($_SESSION['admin']);
session_destroy();
header('Location: index.php');
Once the session is invalidated I want to open the page index.php
.
But I am geting the following error:
Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in C:\xampp\htdocs\Selection\logout.php on line 3
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\Selection\logout.php:3) in C:\xampp\htdocs\Selection\logout.php on line 4
What is wrong?
I think that you can't have called the session_start()
function before destroy the session.
You`ll need to call session_start() on top of the page to remind php that this pagecall belongs to the session. - At least PHP manual tells that.
The notes on that manual page give hint, that session_unset() is only to be used in older environments that are not using $_SESSION variable.
You have to open the session first:
header('Location: index.php');
session_start();
session_unset();
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