session_destroy
session_destroy(): bool
销毁存储中的会话数据(登出语义);不清空 $_SESSION 超全局,需自行置空。
C C23
C23
#include <stdio.h>
#include <string.h>
struct KV {
char key[32];
char val[64];
};
static const char *kv_get(struct KV *store, int n, const char *key) {
for (int i = 0; i < n; i++) {
if (strcmp(store[i].key, key) == 0) return store[i].val;
}
return NULL;
}
int main(void) {
/* 对照语义:销毁存储数据(计数清零);重开为空 */
/* PHP 的 session_destroy 只删存储不动 $_SESSION;这里的清零相当于「两件事一起做」 */
struct KV store[2];
int n = 1;
strcpy(store[0].key, "token");
strcpy(store[0].val, "abc");
printf("token: %s\n", kv_get(store, n, "token"));
n = 0; /* 销毁 */
printf("destroyed: %s\n", n == 0 ? "yes" : "no");
printf("reopened: %s\n", n == 0 ? "empty" : "stale");
return 0;
}
输出
token: abc destroyed: yes reopened: empty
C++ C++26
C++26
#include <iostream>
#include <map>
#include <string>
int main() {
// 对照语义:销毁存储数据;重开为空
// PHP 的 session_destroy 只删存储不动 $_SESSION;这里的 clear 相当于「两件事一起做」
std::map<std::string, std::string> store{{"token", "abc"}};
std::cout << "token: " << store["token"] << "\n";
store.clear();
std::cout << "destroyed: " << (store.empty() ? "yes" : "no") << "\n";
std::cout << "reopened: " << (store.empty() ? "empty" : "stale") << "\n";
return 0;
}
输出
token: abc destroyed: yes reopened: empty
C# v11.0.100
v11.0.100
// 对照语义:销毁存储数据;重开为空
// PHP 的 session_destroy 只删存储不动 $_SESSION;这里的 Clear 相当于「两件事一起做」
var store = new Dictionary<string, string> { ["token"] = "abc" };
Console.WriteLine($"token: {store["token"]}");
store.Clear();
Console.WriteLine($"destroyed: {(store.Count == 0 ? "yes" : "no")}");
Console.WriteLine($"reopened: {(store.Count == 0 ? "empty" : "stale")}");
输出
token: abc destroyed: yes reopened: empty
Go v1.27.4
v1.27.4
package main
import "fmt"
func main() {
// 对照语义:销毁存储数据;重开为空
// PHP 的 session_destroy 只删存储不动 $_SESSION;这里的重建相当于「两件事一起做」
store := map[string]string{"token": "abc"}
fmt.Println("token:", store["token"])
store = map[string]string{}
fmt.Println("destroyed:", map[bool]string{true: "yes", false: "no"}[len(store) == 0])
fmt.Println("reopened:", map[bool]string{true: "empty", false: "stale"}[len(store) == 0])
}
输出
token: abc destroyed: yes reopened: empty
Java v27.0.1
v27.0.1
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// 对照语义:销毁存储数据;重开为空(Servlet 里对应 session.invalidate())
// PHP 的 session_destroy 只删存储不动 $_SESSION;这里的 clear 相当于「两件事一起做」
Map<String, String> store = new HashMap<>();
store.put("token", "abc");
System.out.println("token: " + store.get("token"));
store.clear();
System.out.println("destroyed: " + (store.isEmpty() ? "yes" : "no"));
System.out.println("reopened: " + (store.isEmpty() ? "empty" : "stale"));
}
}
输出
token: abc destroyed: yes reopened: empty
JavaScript v26.13.1
v26.13.1
// 对照语义:销毁存储数据;重开为空
// PHP 的 session_destroy 只删存储不动 $_SESSION;这里的 clear 相当于「两件事一起做」
const store = new Map([["token", "abc"]]);
console.log(`token: ${store.get("token")}`);
store.clear();
console.log(`destroyed: ${store.size === 0 ? "yes" : "no"}`);
console.log(`reopened: ${store.size === 0 ? "empty" : "stale"}`);
输出
token: abc destroyed: yes reopened: empty
PHP v8.6.0
v8.6.0
// session_destroy:删除存储中的会话数据(登出语义)
ini_set("session.save_path", sys_get_temp_dir());
session_start();
$_SESSION["token"] = "abc";
echo $_SESSION["token"], "\n";
session_write_close();
session_start();
echo $_SESSION["token"], "\n"; // 重新打开:数据还在
// 官方登出惯例:清空超全局 + 销毁存储——destroy 不会替你清 $_SESSION,
// 也不影响已发给浏览器的会话 Cookie(要彻底登出可再 session_regenerate_id 或清 Cookie)
$_SESSION = [];
session_destroy();
echo session_status() === PHP_SESSION_ACTIVE ? "active\n" : "inactive\n";
session_start();
echo isset($_SESSION["token"]) ? "kept\n" : "cleared\n";
session_destroy();
输出
abc abc inactive cleared
Python v3.15.2
v3.15.2
# 对照语义:销毁存储数据;重开为空
# PHP 的 session_destroy 只删存储不动 $_SESSION;这里的 clear 相当于「两件事一起做」
store = {"token": "abc"}
print("token:", store["token"])
store.clear()
print("destroyed:", "yes" if not store else "no")
print("reopened:", "empty" if not store else "stale")
输出
token: abc destroyed: yes reopened: empty
Rust v1.99.1
v1.99.1
use std::collections::HashMap;
fn main() {
// 对照语义:销毁存储数据;重开为空
// PHP 的 session_destroy 只删存储不动 $_SESSION;这里的 clear 相当于「两件事一起做」
let mut store = HashMap::new();
store.insert("token", "abc");
println!("token: {}", store["token"]);
store.clear();
println!("destroyed: {}", if store.is_empty() { "yes" } else { "no" });
println!("reopened: {}", if store.is_empty() { "empty" } else { "stale" });
}
输出
token: abc destroyed: yes reopened: empty
TypeScript v26.13.1
v26.13.1
// 对照语义:销毁存储数据;重开为空
// PHP 的 session_destroy 只删存储不动 $_SESSION;这里的 clear 相当于「两件事一起做」
const store = new Map<string, string>([["token", "abc"]]);
console.log(`token: ${store.get("token")}`);
store.clear();
console.log(`destroyed: ${store.size === 0 ? "yes" : "no"}`);
console.log(`reopened: ${store.size === 0 ? "empty" : "stale"}`);
输出
token: abc destroyed: yes reopened: empty
语言差异说明
C:销毁即计数清零/结构体重置;真正的「登出」还要清浏览器 Cookie(手发 Set-Cookie 过期头)。
C++:同 C,clear() 即销毁。
C#:Dictionary.Clear() 对应销毁;ASP.NET 里是 HttpContext.SignOutAsync()/Session.Clear(),框架自动处理 Cookie。
Go:重建空 map;gorilla/sessions 里是 session.Options.MaxAge = -1 + session.Save(让浏览器删 Cookie)。
Java:Map.clear() 对应销毁;Servlet 里是 session.invalidate()——容器一并使会话 ID 失效。
JavaScript:Map.clear() 对应销毁;express-session 里是 req.session.destroy(cb)(服务端删存储),浏览器 Cookie 还需 res.clearCookie。
PHP:session_destroy 只删存储中的会话数据,三件事它不做:不清 $_SESSION(要自己 $_SESSION = [])、不删会话 Cookie(浏览器带着旧 ID 再来会开新会话)、不动其他会话;登出完整惯例是 unset/清空 + destroy + (可选)清 Cookie。
Python:dict.clear() 对应销毁;Flask 里是 session.clear()(签名 Cookie 方案,客户端即存储)。
Rust:HashMap::clear 对应销毁。
TypeScript:同 JavaScript。