"Trying to destroy uninitialized session"

I’m getting that error when i try to end a session.

Basically, i was following the webmasterbase.com article on building a login script found here: http://www.webmasterbase.com/article/319

I finished it and i made a logout script using this code:

<?php // logout.php

include ('head.php');
include ('join_header.php');

session_unregister($username);
session_unregister($password);
session_destroy();

?>
<center>
<font size="2">
Logout Sucessful!
Please <a href="/resources">re-login</a> to access the site again.
</font>
</center>
<?php
include ('join_footer.php');
?>

The way i’m doing this is, i have the accesscontrol included in my script. i login, and then i creates a link to the logout script. Yet, when i click the logout link, i get this:

Warning: Trying to destroy uninitialized session in /usr/local/plesk/apache/vhosts/3dartisan.net/httpdocs/logout.php on line 8

the session is started in the accesscontrol.php

any ideas why session_destroy(); isn’t working?

You’ll have to call session_start() before you call session_destroy();

Are you storing session in data in files or in a database. If you are storing it in a database, I normally just delete the record from the session table that corresponds to the session id, that way you don’t have to unregister each session var and it actually deletes the whole session.

Originally posted by Defender1
the session is started in the accesscontrol.php
I said it right above there. And i’m using php’s built in sessions. i’m guessing it’s files. if you read the tutorial’s second section, you’ll see EXACTLY how the session’s are being used and where session_start() is called

the session is started in the accesscontrol.php
any ideas why session_destroy(); isn’t working?

I looked again, could you show me exactly where in your code you include accesscontrol.php. Cause I sure don’t see it.

If the session is started in one script and then based off a session variable you send them to another script, you must use session_start() in every script you want to use session functions. Furthermore I suggest you store session data in a database, http://www.phpbuilder.com/columns/ying20000602.php3 its faster cause you don’t need to open files to get session data. And you can simply delete the record holding the session data when you want to delete a session.

I learned sessions from php.net so I am not familiar with the tutorial you speak of.

thats cause i changed it’s name to verify_access.php
i never use file names that are in articles for security reasons :slight_smile:

well, if you read the tutorial that i linked above, it wouldn’t be difficult.
but i’m curious why sitepoint’s article wouldn’t work. oh well.
i’ll try out the db method.

In the code that you posted above, you never include an accesscontrol or your version of it, verify_access. The only files you include are join_footer, head, and join_header. Try including verify_access and it may work.