Skip to content

Commit

Permalink
Add function session_destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanguangcheng committed Dec 8, 2023
1 parent 6ac89ea commit da3dc5e
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion php.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ memory_limit = 512M
opcache.jit_buffer_size=128M
opcache.jit=tracing

disable_functions=set_time_limit,header,header_remove,headers_sent,headers_list,http_response_code,setcookie,setrawcookie,session_start,session_id,session_name,session_save_path,session_status,session_write_close,session_regenerate_id,session_unset
disable_functions=set_time_limit,header,header_remove,headers_sent,headers_list,http_response_code,setcookie,setrawcookie,session_start,session_id,session_name,session_save_path,session_status,session_write_close,session_regenerate_id,session_unset,session_destroy
18 changes: 18 additions & 0 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ function session_write_close(): bool
$session->setData($_SESSION);
$session->save();
}
Http::$sessionIsStarted = false;
return true;
}

Expand Down Expand Up @@ -336,6 +337,23 @@ function session_unset(): bool
return false;
}

/**
* Destroy session
*
* @return bool
*/
function session_destroy(): bool
{
if (\session_status() === \PHP_SESSION_ACTIVE) {
$request = Http::$request;
$request->session->flush();
$request->session->save();
Http::$sessionIsStarted = false;
return true;
}
return false;
}

/**
* Replace the exit() call with exit_exception() to avoid program exit
* If status is a string, function exit() will print the status just before exiting.
Expand Down
1 change: 0 additions & 1 deletion src/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public static function encode($response, TcpConnection $connection): string
{
if (static::$sessionIsStarted) {
\session_write_close();
static::$sessionIsStarted = false;
}
if ($response instanceof \Workerman\Protocols\Http\Response) {
return parent::encode($response, $connection);
Expand Down
3 changes: 2 additions & 1 deletion src/Linkerman.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

class Linkerman
{
const VERSION = '0.1.0';
const VERSION = '0.1.1';

const REWRITE_FUNCTIONS = [
'set_time_limit',
Expand All @@ -33,6 +33,7 @@ class Linkerman
'session_write_close',
'session_regenerate_id',
'session_unset',
'session_destroy',
];

public static function init(): void
Expand Down
10 changes: 10 additions & 0 deletions tests/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,16 @@ function sessions(): string
return json_encode($_SESSION);
}
})(),
'session-destroy' => (static function () {
session_start();
$_SESSION['name'] = 'linkerman';
$result[] = session_destroy();
$result[] = session_status();
$file = session_save_path() . '/session_' . session_id();
$result[] = empty($_SESSION);
$result[] = file_exists($file);
return json_encode($result);
})(),
};
}

Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
expect($response->getBody()->getContents())->toBe('[]');
});

test("tests session_destroy", function() {
$response = HttpClient()->get('/sessions', [
'query' => ['type' => 'session-destroy']
]);
expect($response->getBody()->getContents())->toBeJson()->json()->toBe([true, 0, false, false]);
});

test("tests session_name", function() {
$response = HttpClient()->get('/sessions', [
'query' => ['type' => 'session-name']
Expand Down

0 comments on commit da3dc5e

Please sign in to comment.