PHP 5.6.38 Released

session_destroy

(PHP 4, PHP 5, PHP 7)

session_destroy세션에 등록된 모든 데이터 파괴

설명

bool session_destroy ( void )

session_destroy()는 현재 세션에 관련된 모든 데이터를 파괴합니다. 세션에 연관된 전역 변수나 세션 쿠키를 해제하지는 않습니다. 다시 세션 변수를 사용하려면, session_start()를 호출해야 합니다.

사용자에게 출력된 기록을 포함하여 모든 세션을 없애려면, 세션 id를 해제해야 합니다. 세션 id가 쿠키로 사용되면(기본 동작), 세션 쿠키를 지워야 합니다. 이를 위해 setcookie()를 사용할 수 있습니다.

반환값

성공 시 TRUE를, 실패 시 FALSE를 반환합니다.

예제

Example #1 $_SESSION으로 세션 파괴하기

<?php
// 세션 초기화
// session_name("something")을 사용한다면, 잊지 마십시오!
session_start();

// 모든 세션 변수 해제
$_SESSION = array();

// 세션을 없애려면, 세션 쿠키도 지웁니다.
// 주의: 이 동작은 세션 데이터뿐이 아닌, 세션 자체를 파괴합니다!
if (isset($_COOKIE[session_name()])) {
    
setcookie(session_name(), ''time()-42000'/');
}

// 마지막으로, 세션 파괴.
session_destroy();
?>

주의

Note:

$_SESSION을 사용하지 않는 오랜 배제된 코드에서는 session_unset()을 사용하십시오.

참고

add a note add a note

User Contributed Notes 6 notes

up
45
Praveen V
6 years ago
If you want to change the session id on each log in, make sure to use session_regenerate_id(true) during the log in process.

<?php
session_start
();
session_regenerate_id(true);
?>

[Edited by moderator (googleguy at php dot net)]
up
25
Jack Luo
4 years ago
It took me a while to figure out how to destroy a particular session in php. Note I'm not sure if solution provided below is perfect but it seems work for me. Please feel free to post any easier way to destroy a particular session. Because it's quite useful for functionality of force an user offline.

1. If you're using db or memcached to manage session, you can always delete that session entry directly from db or memcached.

2. Using generic php session methods to delete a particular session(by session id).

<?php
$session_id_to_destroy
= 'nill2if998vhplq9f3pj08vjb1';
// 1. commit session if it's started.
if (session_id()) {
   
session_commit();
}

// 2. store current session id
session_start();
$current_session_id = session_id();
session_commit();

// 3. hijack then destroy session specified.
session_id($session_id_to_destroy);
session_start();
session_destroy();
session_commit();

// 4. restore current session id. If don't restore it, your current session will refer to the session you just destroyed!
session_id($current_session_id);
session_start();
session_commit();

?>
up
2
JBH
1 year ago
I'm using PHP 7.1 and received the following warning when implementing Example #1, above:

    PHP message: PHP Warning:  session_destroy(): Trying to destroy uninitialized session in...

What I discovered is that clearing $_SESSION and removing the cookie destroys the session, hence the warning.  To avoid the warning while still keeping the value of using session_destroy(), do this after everything else:

    if (session_status() == PHP_SESSION_ACTIVE) { session_destroy(); }
up
-8
Colin
11 years ago
Note that when you are using a custom session handler, session_destroy will cause a fatal error if you have set the session destroy function used by session_set_save_handler to private.

Example:
Fatal error: Call to private method Session::sessDestroy()

where sessDestroy was the function I specified in the 5th parameter of session_set_save_handler.

Even though it isn't all that desirable, the simple solution is to set sessDestroy to public.
up
-10
Gaurav
2 years ago
For session_destroy() only destroy current session mean that if you specify name or change the save path of session etc  ,it will not destroy it mean for example

create.php

<?php
session_name
('testing') ;
session_start() ;

$_SESSION['id'] = '35' ;
?>

delete.php
<?php
session_start
() ;

session_destroy() ;
?>

session_destroy only delete the new session which is created by session_start(). correct way is
<?php
session_name
('testing') ;
session_start() ;

session_destroy() ;
?>

this is also valid for if you change session.save path throught ini_set() , you have to mention in  delete.php.
remember session_destroy() function destroy  only current session not all .i hope this is worth to mention.
up
-56
administrator at anorhack dot com
11 years ago
Destroying  a session from a background job

I have a thief-protection system that compares country codes from login IPs via whois. This has to run in the background as it is way too processor-hungry to be run in the browser.

What I needed was a way to destroy the web session from the background job. For some reason, a background session_destroy APPEARS to work, but doesnt't actually destroy the web session.

There is a work around, I set the username to NULL and the web code picks up on that, bouncing the user (thief) to a "gotcha" page where his IP is logged.

Yes I know its nasty and dirty, but surprisingly it works.

$sid = the session_id() of the suspicious web session, passed in $argv to the background job

The trick is to "stuff" the $_GET array with the sid, then the session_start in the background job picks this value up (as if it were a genuine trans-sid type thing...?PHPSESSID=blah) and "connects to" the web session. All $_SESSION variable can be viewed (and CHANGED , which is how this kludge works) but for some reason (that no doubt someone will illuminate) they can't be unset...setting the particular variable to NULL works well though:


$_GET[session_name()]=$sid;
session_start();
// prove we are getting the web session data
foreach($_SESSION as $k => $v) echo($k."=".$v);
// now kill the thief
$_SESSION['username']=NULL;
//web session variable now NULL - honestly!
To Top