PHP session issues

I am using the following code to cancel a session. I am associated with logout.php on many pages. If you click this link to log out, the logout.php page is logout.php . Below is the code in logout.php .

 unset($_SESSION['admin']); session_destroy(); header('Location: index.php'); 

Once the session is invalid, I want to open the index.php page. But I get the following error:

Warning: session_destroy () [function.session-destroy]: attempt to destroy an uninitialized session in C: \ xampp \ htdocs \ Selection \ logout.php on line 3

Warning: unable to change header information - headers already sent (the output is running in C: \ xampp \ htdocs \ Selection \ logout.php: 3) in C: \ xampp \ htdocs \ Selection \ logout.php on line 4

What's wrong?

+6
php session
source share
5 answers

I think you cannot call the session_start() function before destroying the session.

+16
source share

You will need to call session_start () at the top of the page to remind php that this pagecall belongs to the session. - At least the PHP manual reports this.

The notes on this man page give a hint that session_unset () is only used in older environments that do not use the $ _SESSION variable.

+8
source share

First you need to open a session:

 header('Location: index.php'); session_start(); session_unset(); session_destroy(); 
+2
source share

The problem is that you cannot destroy a session that was not started. Then a warning occurs that repeats in the browser. The next problem is that you cannot send headers after exiting the browser, so it triggers another warning.

You just need to check if the first session exists:

 if (session_name() != '') { session_destroy(); } 
+1
source share

You should ALWAYS use session_start (); BEFORE you start using a function / session variable. So run all the PHP files using session_start () ;. Also logout.php:

 session_start(); session_destroy(); header('Location: index.php'); 

You also do not need to disable it.

0
source share

All Articles