★ wanayoo — archive 1999 http://www.php.net/manual/function.session-destroy.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Sessions
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Sessions
* session_start
* session_destroy
* session_name
* session_module_name
* session_save_path
* session_id
* session_register
* session_unregister
* session_unset
* session_is_registered
* session_get_cookie_params
* session_set_cookie_params
* session_decode
* session_encode
Manual: session_destroy
View the source code for this pageSearch the site



Previous page
 session_start
 Updated
Sat, 12 Aug 2000
session_name 
Next page


session_destroy

(PHP4 )

session_destroy -- Destroys all data registered to a session

Description

bool session_destroy(void);

session_destroy() destroys all of the data associated with the current session.

This function returns true on success and false on failure to destroy the session data.


User Contributed Notes: session_destroy


what about session_end?
26-Feb-2000 01:39
This function only wipes the registered session variables - it does not, however, kill the cookies passing session variables and stuff. So, what happens when i want to have a page on the site start with NO session?


cedric.chandon@supelec.fr
17-Mar-2000 11:14
it doesn't seem that an auto-destroy after a specified delay is done: example:
method witout cookies:
an user come and log on a site, he receives an ID, and some variable like his acces are stored. If he bookmark his adresse (with ID=5665456423456), and he come back 2 days after, he is still logged...
I really need to have an end_session after a customizable periode



bhoehne@ifleet.com
12-Apr-2000 04:20
If I run the following code with an active session that has registered elements :
<PRE>
if (session_destroy()) {
echo "session destroyed
";
} else {
echo "session not destroyed
";
}
</PRE>
what gets displayed is :
<PRE>
session not destroyed
</PRE>
This would indicate that the session_destroy() function is returning false. Am I missing something?



index@idxsi.qc.ca
17-Apr-2000 10:29
Difficulties with destroy!!!
I'm new with session but I think I'm able to make a login page that ask for user and password, verify it in a database, begin a session (I could see a new session file in the /tmp but I can't do a session_destroy(). I got the "Trying to destroy uninitialised session in /../logout.php on line x"
Could you give me more about this situation? Where do I have to look for more information. How can I solve this?



roche@ensicaen.ismra.fr
20-Apr-2000 10:51
Two problems appears with session management:

1. I first open the session with session_start(), check if the session is okay ($myobject class), and if not I do:
session_destroy();
session_id(somerandomID);
session_start();
$myobject=new MyObject();

PROBLEM: The first time a client loggs in, no cookie is being sent. The session is a default session with a default ID - so the ID is changed, and session reopened. BUT The "Set-Cookie:" gives two cookies with same name (WID=<first default cookie>,WID=<the new one>) and of course the browser has some problems handling two cookies wih same name!

2. there is no "lifetime" variable that can be used. so I use a class, and in the constructor I just add:

$this->time=time(); /* time on current date */
$this->lifetime=30; /* max time */

And in the begining of the script:

if ($session->lifetime > 0) {
if ( (time() - $session->time) > $session->lifetime) {
/* cookie expired */
session_destroy();
}
}

It's working fine - but is there a simpler way?



telescope7@hotmail.com
10-May-2000 11:48
Alright. Session_destroy is the most evil of all functions. I am trying to do a login page which prompts for a login and passwd on the main web page and uses a post/form into another php script that validates the username and passwd(the username is stored in session_register). If it fails, the script calls back up the initial login screen, if it is a success it then proceeds into the website. I am getting very unpredictable results. Is there something wrong with PHP and forms/post or are cookies getting messed up or what.

Dazed and Confused -- Matt



stephaneeybert@hotmail.com
18-May-2000 12:48
Help! I can't destroy the session.
Enviroment is PHP4 on Linux/Apache.
Function is:
function closeSession() {
// The next source code line is needed to avoid a warning saying the session is uninitialized, but I don't want that line there!
// And anyway, it doesn't destroy my session.
// session_start();

// Displays the session ID, I can see it!
global $PHPSESSID;
print("ID " . $PHPSESSID);

// To close the session
if (session_destroy()) {
echo "
Session destroyed
";
} else {
// It always displays NOT destroyed
echo "
Session NOT destroyed
";
}

// Displays the session ID, I can see it!
print("ID " . $PHPSESSID);
}




chris@digitaria.com
26-Jun-2000 03:38
I've entered bug #5231 to report that session_destroy() basically doesn't do a thing. Workarounds are included.


toledo@kingwoodcable.com
01-Jul-2000 03:31
I am extremely new to PHP, however, I think I have unravelled the mystery of session_destroy. First of all, it is returning false, regardless of what the manual entry says. However, it appears to be doing it's job anyway. I'm using PHP 4.0.1 on a Solaris box running Apache 1.3.12, and session information is stored in files within the /tmp directory. When session_destroy is called, the relevant info file is removed from /tmp, effectively wiping out all information relevant to the session, just like the manual entry says. However, what session_destroy doesn't do is completely *end* the session; any cookies that reference the session still remain, and, if cookies weren't used, any URLs that contain the session ID will cause the session info file to be recreated, albeit without all its previous information.

So... like I said, I'm new at this. Anybody know of a way to guarantee that a session is terminated so that any future references to it will generate an error?



session_destroy_should_be_rewritten@xcene.flipp.net
05-Jul-2000 02:58
(in re: to bug id #5231).


I guess what you want is a way to be 100% sure that on certain occasions, session_start will definitively open a new session.


I find this to work well:


<pre>
&lt;?
// kick off any old session..
@session_start();
@session_destroy();

// open a brand new session...
@session_start();
// blablabla get on with it.
</pre>


The reason for opening the session twice is that it seems you cannot do session_destroy unless you already have it open. Or something.
As someone mentioned earlier, session_start should take a parameter such as "create_new_session" or "resume_old_session".



session_destroy_didnt_do_the_magic@xcene.flipp.net
05-Jul-2000 03:09
well, seems the trick in the previous post isn't good for anything after all.



matt@daart.ca
10-Jul-2000 09:43
To get around this odd behavior of session_destroy not being reliable, I've simply set a session variable to true when some one is signed in and then later I'll set it to false when they are logging out.

sessions do seem to be destroyed reliably when the browser is closed, however.



ssross@uswest.net
02-Aug-2000 09:46
As noted earlier, this function only destroys the data, not the cookie. I found this to work for me.
<pre>
session_destroy();
$p = session_get_cookie_params();
setcookie(session_name(), "", 0, $p["path"], $p["domain"]);
</pre>



rayhar@kc.rr.com
05-Aug-2000 06:18
If you've read the newly published SAMS book, "Teach Yourself PHP4 in 24 Hours" by Matt Zandstra (the other PHP4 books are not yet published), and the documentation on PHP's web-site, and became as frustrated as I did trying to get "sessions" to work, I hope my description here will help.

The example on page 340 in SAMS book does not work.


A simplified example (according to the book) is:


Here's the first page:

testsession1.php:
=====================================================
&lt;?php

session_start();

session_register("fname");
session_register("lname");

$fname = "Mickey";
$lname = "Mouse";

print session_id()."
";
print session_encode()."
";
print session_save_path()."
";

?>
=====================================================


Browsing testsession1.php sets the session variables.


Here is what is displayed in Browser (testsession1.php)


6ef4f3c6ff1bab0d7b1f274c2f538c5e
fname|s:6:"Mickey";lname|s:5:"Mouse";
/tmp



Here's the next page:

testsession2.php
=====================================================
&lt;?php

session_start();

print $fname;
print "
";
print $lname;

print session_save_path();

?>
=====================================================


Supposedly, browsing the second page will get and display the session variables. However, it does not work. A separate session is started.


Here is what is displayed in the browser (testsession2)


/tmp



Looking at the "/tmp" directory on the Linux/Unix server reveals what happened in the directory:


Here is the result from the first "testsession1.php" page:

[root@www /tmp]# ls -la
total 4
drwxrwxrwt 2 root root 2048 Aug 4 22:41 .
drwxr-xr-x 16 root root 1024 Apr 17 04:16 ..
srwxrwxrwx 1 postgres postgres 0 Jun 1 16:44 .s.PGSQL.5583
srwxrwxrwx 1 root root 0 Jun 12 14:55 mysql.sock
-rw------- 1 httpd httpd 37 Aug 4 22:41 sess_6ef4f3c6ff1bab0d7b1f274c2f538c5e


Here is the result from the second "testsession2.php" page:

[root@www /tmp]# ls -la
total 4
drwxrwxrwt 2 root root 2048 Aug 4 22:44 .
drwxr-xr-x 16 root root 1024 Apr 17 04:16 ..
srwxrwxrwx 1 postgres postgres 0 Jun 1 16:44 .s.PGSQL.5583
srwxrwxrwx 1 root root 0 Jun 12 14:55 mysql.sock
-rw------- 1 httpd httpd 37 Aug 4 22:41 sess_6ef4f3c6ff1bab0d7b1f274c2f538c5e
-rw------- 1 httpd httpd 0 Aug 4 22:44 sess_82a894a3816e83f38268fd36695fccc2



You can see that the second page did not access the original session and its variables. It created a new session and hence a new session/cookie/file on the server.



*** HERE IS WHAT WORKS *** -- the example on page 345 of SAMS book:


Here is the first page...


testsession1.php:
=====================================================
&lt;?php

session_start();

session_register("fname");
session_register("lname");

$fname = "Mickey";
$lname = "Mouse";

print session_id()."
";
print session_encode()."
";
print session_save_path()."
";


?>

<A HREF=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Btestsession2.php%3F%26amp%3Blt%3B%3F&y=1999 print SID; ?>">Next Page</A>
=====================================================



Here is the second page...

testsession2.php:
=====================================================
&lt;?php

session_start();

print $fname;
print "
";
print $lname;
print "
";


print session_save_path();

?>
=====================================================


When you click on the link to the next page, here is the ADDRESS in the browser:

<WEBSITE_URL>/testsession2.phtml?PHPSESSID=d66198107f6cfaa1dee0f657a286feb2


Here is what is displayed in the browser.....

Mickey
Mouse
/tmp


As you can see, the variable "SID" was magically changed to the variable "PHPSESSID", and the second page magically opened the session with the session_start() function.

I hope this helps.



kahnmatthew@hotmail.com
05-Aug-2000 08:08
In order to make <b>session_destroy()</b> work, you must first invoke <b>session_start()</b>. Doing this will activate the current session so that it can be destroyed. It will remove the cookie (i.e. disabling the use of the browser back button to view cached pages) as well as the active session. A new call to <b>session_start()</b> will begin a new session.


 About Notes


Previous page
 session_start
 Updated
Sat, 12 Aug 2000
session_name 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.