session_start

session_start(array $options = []): bool

Starts the session (also sends the session cookie under Web SAPI); afterwards session data is read/written via $_SESSION.

C C23

C23
#include <stdio.h>
#include <string.h>

/* KV storage entry: the minimal equivalent of in-process key-value state */
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 sessions are a framework-level concept; at the language level only in-process key-value storage can be demonstrated */
    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;
}
Output
active
alice
1
closed

C++ C++26

C++26
#include <iostream>
#include <map>
#include <string>

int main() {
    // Web sessions are a framework-level concept; at the language level only in-process key-value storage can be demonstrated
    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;
}
Output
active
alice
1
closed

C# v11.0.100

v11.0.100
// Web sessions are a framework-level concept (ASP.NET Session/HttpContext.Session is the real equivalent); the language level demos in-process key-value storage
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");
Output
active
alice
1
closed

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// Web sessions are a framework-level concept (gorilla/sessions is the real equivalent); the language level demos in-process key-value storage
	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")
}
Output
active
alice
1
closed

Java v27.0.1

v27.0.1
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        // Web sessions are a framework-level concept (Servlet HttpSession is the real equivalent); the language level demos in-process key-value storage
        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");
    }
}
Output
active
alice
1
closed

JavaScript v26.13.1

v26.13.1
// Web sessions are a framework-level concept (express-session is the real equivalent); the language level demos in-process key-value storage
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");
Output
active
alice
1
closed

PHP v8.6.0

v8.6.0
// session_start: starts the session; under a Web SAPI it also sends Set-Cookie: PHPSESSID=...
// The CLI demo points the save path at the system temp directory (works in containers/CI alike)
ini_set("session.save_path", sys_get_temp_dir());
session_start();
$_SESSION["user"] = "alice";
$_SESSION["visits"] = 1;
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";
// Commit and close (releases the session file lock)
session_write_close();
echo session_status() === PHP_SESSION_ACTIVE ? "active" : "closed";
Output
active
alice
1
has id
closed

Python v3.15.2

v3.15.2
# Web sessions are a framework-level concept (Flask's session is the real equivalent); the language level demos in-process key-value storage
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")
Output
active
alice
1
closed

Rust v1.99.1

v1.99.1
use std::collections::HashMap;

fn main() {
    // Web sessions are a framework-level concept (axum-session and friends are the real equivalents); the language level demos in-process key-value storage
    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" });
}
Output
active
alice
1
closed

TypeScript v26.13.1

v26.13.1
// Web sessions are a framework-level concept (express-session is the real equivalent); the language level demos in-process key-value storage
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");
Output
active
alice
1
closed

Language differences

C: no session concept — the nearest equivalent is an in-process key-value structure (a struct array here); HTTP sessions mean hand-rolling cookie parsing plus server-side storage.

C++: same as C with a std::map; framework-grade sessions live in Crow/Drogon-style libraries.

C#: the real equivalent is ASP.NET's HttpContext.Session (framework layer, Redis for distributed stores); at the language level there's only an in-process Dictionary.

Go: the real equivalent is gorilla/sessions-style libraries; at the language level only an in-process map — PHP's session data survives across requests, an in-process map does not.

Java: the real equivalent is Servlet HttpSession (container-managed, transparent cookie/URL rewriting); at the language level only a HashMap.

JavaScript: the real equivalent is express-session (server-side store plus cookie session ID); at the language level only a Map. Browser localStorage is a different dimension.

PHP: session_start does it in one step — reads the session ID from the cookie, loads storage into $_SESSION, locks the session file, and under Web SAPI also sends Set-Cookie; session_status() has three states (DISABLED/NONE/ACTIVE); it also runs on CLI (data lands in save_path) — the example uses sys_get_temp_dir() to guarantee writability.

Python: the real equivalent is Flask's session (signed-cookie approach); at the language level only a dict.

Rust: no standard sessions — a web framework (axum/actix) with session middleware is the real equivalent; at the language level only a HashMap.

TypeScript: same as JavaScript.