session_destroy()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

session_destroy()

Post by aravona »

I have a working login - and I want a log out button.

What I've got for my button is:

Code: Select all

<form action='logout.php' method='post'><input name='Logout' type='Submit' value='Logout' /></form>
And logout.php:

Code: Select all

<?php   
session_destroy(); 
echo "You have logged out";
echo "<head><meta HTTP-EQUIV='REFRESH' content='10; url=index.html'></head>";
?>
But its not working, it gives this: Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in D:\wamp\wamp\www\stuff\logout.php on line 2

My session is started elsewhere, so what do I need to do in order to destroy my session and get my user to log out?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: session_destroy()

Post by requinix »

You have to start the session before you can destroy it.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: session_destroy()

Post by aravona »

ok lol, that seems a bit odd but it did work :) my user does log out now :)

Thank you kindly :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: session_destroy()

Post by requinix »

aravona wrote:ok lol, that seems a bit odd but it did work
PHP doesn't know which session it should destroy until you call session_start. That's when it looks at the user's cookies to look up the session to use.
Post Reply