Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP session problems

Tags:

php

session

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?

like image 624
user156073 Avatar asked Aug 30 '09 13:08

user156073


3 Answers

I think that you can't have called the session_start() function before destroy the session.

like image 146
mck89 Avatar answered Oct 02 '22 15:10

mck89


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.

like image 44
Oliver Friedrich Avatar answered Oct 02 '22 13:10

Oliver Friedrich


You have to open the session first:

header('Location: index.php');
session_start();
session_unset();
session_destroy();
like image 41
rodrigoap Avatar answered Oct 02 '22 13:10

rodrigoap