session_start
session_start(array $options = []): bool
启动会话(Web SAPI 下同时下发会话 Cookie),之后可通过 $_SESSION 读写会话数据。
C C23
#include <stdio.h>
#include <string.h>
/* KV 存储条目:进程内键值状态的最简等价物 */
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) {
/* Web 会话是框架层概念;语言级只能演示进程内键值存储——无跨请求持久化 */
struct KV store[8];
int n = 0;
strcpy(store[n].key, "user"); strcpy(store[n].val, "alice"); n++;
strcpy(store[n].key, "visits"); strcpy(store[n].val, "1"); n++;
printf("%s\n", n > 0 ? "active" : "inactive");
printf("%s\n", kv_get(store, n, "user"));
printf("%s\n", kv_get(store, n, "visits"));
n = 0;
printf("closed\n");
return 0;
}
active alice 1 closed
C++ C++26
#include <iostream>
#include <map>
#include <string>
int main() {
// Web 会话是框架层概念;语言级只能演示进程内键值存储——无跨请求持久化
std::map<std::string, std::string> store;
store["user"] = "alice";
store["visits"] = "1";
std::cout << (store.empty() ? "inactive" : "active") << "\n";
std::cout << store["user"] << "\n";
std::cout << store["visits"] << "\n";
store.clear();
std::cout << (store.empty() ? "closed" : "open") << "\n";
return 0;
}
active alice 1 closed
C# v11.0.100
// Web 会话是框架层概念(ASP.NET Session/HttpContext.Session 才是真等价物);语言级只能演示进程内键值存储
// 与 PHP 的差异:这里没有跨请求持久化,也没有 Cookie 往返的会话 ID
var store = new Dictionary<string, object>();
store["user"] = "alice";
store["visits"] = 1;
Console.WriteLine(store.Count == 0 ? "inactive" : "active");
Console.WriteLine(store["user"]);
Console.WriteLine(store["visits"]);
store = new Dictionary<string, object>();
Console.WriteLine("closed");
active alice 1 closed
Go v1.27.4
package main
import "fmt"
func main() {
// Web 会话是框架层概念(gorilla/sessions 才是真等价物);语言级只能演示进程内键值存储
// 与 PHP 的差异:这里没有跨请求持久化,也没有 Cookie 往返的会话 ID
store := map[string]any{}
store["user"] = "alice"
store["visits"] = 1
if len(store) > 0 {
fmt.Println("active")
} else {
fmt.Println("inactive")
}
fmt.Println(store["user"])
fmt.Println(store["visits"])
store = map[string]any{}
fmt.Println("closed")
}
active alice 1 closed
Java v27.0.1
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Web 会话是框架层概念(Servlet HttpSession 才是真等价物);语言级只能演示进程内键值存储
// 与 PHP 的差异:这里没有跨请求持久化,也没有 Cookie 往返的会话 ID
Map<String, Object> store = new HashMap<>();
store.put("user", "alice");
store.put("visits", 1);
System.out.println(store.isEmpty() ? "inactive" : "active");
System.out.println(store.get("user"));
System.out.println(store.get("visits"));
store = new HashMap<>();
System.out.println("closed");
}
}
active alice 1 closed
JavaScript v26.13.1
// Web 会话是框架层概念(express-session 才是真等价物);语言级只能演示进程内键值存储
// 与 PHP 的差异:这里没有跨请求持久化,也没有 Cookie 往返的会话 ID
const session = new Map();
session.set("user", "alice");
session.set("visits", 1);
console.log(session.size > 0 ? "active" : "inactive");
console.log(session.get("user"));
console.log(session.get("visits"));
session.clear();
console.log(session.size === 0 ? "closed" : "open");
active alice 1 closed
PHP v8.6.0
// session_start:启动会话;Web SAPI 下同时发 Set-Cookie: PHPSESSID=...(会话 ID 随 Cookie 往返)
// CLI 演示需把保存路径指到系统临时目录(容器/CI 同样适用)
ini_set("session.save_path", sys_get_temp_dir());
session_start();
$_SESSION["user"] = "alice";
$_SESSION["visits"] = 1;
// session_status() 三态:DISABLED / NONE / ACTIVE
echo session_status() === PHP_SESSION_ACTIVE ? "active\n" : "inactive\n";
echo $_SESSION["user"], "\n";
echo $_SESSION["visits"], "\n";
echo session_id() !== "" ? "has id\n" : "no id\n";
// 提交并关闭(释放会话文件锁)
session_write_close();
echo session_status() === PHP_SESSION_ACTIVE ? "active" : "closed";
active alice 1 has id closed
Python v3.15.2
# Web 会话是框架层概念(Flask 的 session 才是真等价物);语言级只能演示进程内键值存储
# 与 PHP 的差异:这里没有跨请求持久化,也没有 Cookie 往返的会话 ID
session = {}
session["user"] = "alice"
session["visits"] = 1
print("active" if session else "inactive")
print(session["user"])
print(session["visits"])
session.clear()
print("closed" if not session else "open")
active alice 1 closed
Rust v1.99.1
use std::collections::HashMap;
fn main() {
// Web 会话是框架层概念(axum-session 等才是真等价物);语言级只能演示进程内键值存储
// 与 PHP 的差异:这里没有跨请求持久化,也没有 Cookie 往返的会话 ID
let mut store: HashMap<&str, String> = HashMap::new();
store.insert("user", "alice".to_string());
store.insert("visits", "1".to_string());
println!("{}", if store.is_empty() { "inactive" } else { "active" });
println!("{}", store["user"]);
println!("{}", store["visits"]);
store.clear();
println!("{}", if store.is_empty() { "closed" } else { "open" });
}
active alice 1 closed
TypeScript v26.13.1
// Web 会话是框架层概念(express-session 才是真等价物);语言级只能演示进程内键值存储
// 与 PHP 的差异:这里没有跨请求持久化,也没有 Cookie 往返的会话 ID
const session = new Map<string, string | number>();
session.set("user", "alice");
session.set("visits", 1);
console.log(session.size > 0 ? "active" : "inactive");
console.log(session.get("user"));
console.log(session.get("visits"));
session.clear();
console.log(session.size === 0 ? "closed" : "open");
active alice 1 closed
语言差异说明
C:无会话概念——最接近的等价物是进程内键值结构(示例 struct 数组);HTTP 会话要自己实现 Cookie 解析 + 服务端存储。
C++:同 C,std::map 做进程内键值;框架级会话在 Crow/Drogon 等库中。
C#:真正的等价物是 ASP.NET 的 HttpContext.Session(框架层,需分布式存储时用 Redis);语言级只有进程内 Dictionary。
Go:真正的等价物是 gorilla/sessions 等框架库;语言级只有进程内 map——PHP 的会话数据跨请求存活,进程内 map 不行。
Java:真正的等价物是 Servlet HttpSession(容器管理,Cookie/URL 重写透明处理);语言级只有 HashMap。
JavaScript:真正的等价物是 express-session(服务端存储 + Cookie 会话 ID);语言级只有 Map。浏览器端的 localStorage 是另一维度。
PHP:session_start 一步到位:读 Cookie 里的会话 ID、加载存储数据到 $_SESSION、锁会话文件,Web SAPI 下还会补发 Set-Cookie;session_status() 三态(DISABLED/NONE/ACTIVE);CLI 下也能跑(数据落在 save_path),本示例用 sys_get_temp_dir() 保证可写。
Python:真正的等价物是 Flask session(签名 Cookie 方案);语言级只有 dict。
Rust:无标准会话——Web 框架(axum/actix)加 session 中间件才是真等价物;语言级只有 HashMap。
TypeScript:同 JavaScript。