session_name
session_name(?string $name = null): string|false
Reads/writes the session cookie name (default PHPSESSID): must be set before session_start, and the return value is the OLD name - the opposite of intuition.
C C23
#include <stdio.h>
/* C in reverse: a cookie is a struct plus one printf of the Set-Cookie line;
the session name is just a string field */
typedef struct {
const char *name;
const char *value;
const char *path;
int http_only;
} Cookie;
int main(void) {
Cookie c = {"CAPP_SID", "demo-a44", "/", 1};
printf("name: %s value: %s\n", c.name, c.value);
printf("header: %s=%s; Path=%s; HttpOnly\n", c.name, c.value, c.path);
return 0;
}
name: CAPP_SID value: demo-a44 header: CAPP_SID=demo-a44; Path=/; HttpOnly
C++ C++26
#include <iostream>
#include <string>
int main() {
// C++ in reverse: a cookie is a small struct serialized to the
// Set-Cookie line; frameworks wrap this exact shape
struct Cookie {
std::string name;
std::string value;
std::string path;
bool http_only;
};
Cookie c{"CPPAPP_SID", "demo-a44", "/", true};
std::cout << "name: " << c.name << " value: " << c.value << "\n";
std::cout << "header: " << c.name << "=" << c.value
<< "; Path=" << c.path << "; HttpOnly" << "\n";
return 0;
}
name: CPPAPP_SID value: demo-a44 header: CPPAPP_SID=demo-a44; Path=/; HttpOnly
C# v11.0.100
// C# in reverse: System.Net.Cookie models the cookie; ASP.NET auth
// middleware is what actually picks the session cookie's name
var cookie = new System.Net.Cookie("CSAPP_SID", "demo-a44", "/", "example.com") { HttpOnly = true };
Console.WriteLine($"name: {cookie.Name} value: {cookie.Value}");
Console.WriteLine($"path: {cookie.Path} httpOnly: {cookie.HttpOnly}");
name: CSAPP_SID value: demo-a44 path: / httpOnly: True
Go v1.27.4
package main
import (
"fmt"
"net/http"
)
// Go in reverse: the session cookie is whatever http.Cookie you build;
// session libraries just preconfigure Name for you
func main() {
c := http.Cookie{Name: "GOAPP_SID", Value: "demo-a44", Path: "/", HttpOnly: true}
fmt.Println("name:", c.Name)
fmt.Println("value:", c.Value)
fmt.Println("httpOnly:", c.HttpOnly)
}
name: GOAPP_SID value: demo-a44 httpOnly: true
Java v27.0.1
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// Java in reverse: the cookie name lives in container/framework config
// (Tomcat's JSESSIONID, Spring's cookie name property); modeled as data
Map<String, String> cookie = new LinkedHashMap<>();
cookie.put("name", "JAPP_SID");
cookie.put("value", "demo-a44");
cookie.put("path", "/");
cookie.put("httpOnly", "true");
System.out.println("name: " + cookie.get("name") + " value: " + cookie.get("value"));
System.out.println("header: " + cookie.get("name") + "=" + cookie.get("value")
+ "; Path=" + cookie.get("path") + "; HttpOnly");
}
}
name: JAPP_SID value: demo-a44 header: JAPP_SID=demo-a44; Path=/; HttpOnly
JavaScript v26.13.1
// JS in reverse: the cookie name is an express-session option (`name`,
// default connect.sid); the wire format is a plain header string
const header = "APP_SID=demo-a44; Path=/; HttpOnly";
const pair = header.split("; ")[0];
const eq = pair.indexOf("=");
console.log("name:", pair.slice(0, eq));
console.log("value:", pair.slice(eq + 1));
console.log("directives:", header.split("; ").length - 1);
name: APP_SID value: demo-a44 directives: 2
PHP v8.6.0
// session_name: get/set the session cookie name (default PHPSESSID).
// Order matters: set the name BEFORE session_start, and keep every echo
// until after the session work is done (output closes the header phase)
ini_set("session.save_path", sys_get_temp_dir());
$default = session_name();
$previous = session_name("APP_SID"); // returns the OLD name
$current = session_name();
session_start();
$wasActive = session_status() === PHP_SESSION_ACTIVE;
$_SESSION["ok"] = true;
session_write_close();
session_name($previous); // restore once the session is closed
$restored = session_name();
echo "default: $default\n";
echo "previous: $previous\n";
echo "current: $current\n";
echo "started under APP_SID: ", $wasActive ? "yes" : "no", "\n";
echo "restored: $restored\n";
default: PHPSESSID previous: PHPSESSID current: APP_SID started under APP_SID: yes restored: PHPSESSID
Python v3.15.2
# Python in reverse: the cookie name is framework config (Flask's
# SESSION_COOKIE_NAME); stdlib shows the cookie object itself
from http.cookies import SimpleCookie
cookie = SimpleCookie()
cookie["PYAPP_SESSION"] = "demo-a44"
cookie["PYAPP_SESSION"]["path"] = "/"
morsel = cookie["PYAPP_SESSION"]
print("name:", morsel.key)
print("value:", morsel.value)
print("header:", cookie.output().split(";")[0])
name: PYAPP_SESSION value: demo-a44 header: Set-Cookie: PYAPP_SESSION=demo-a44
Rust v1.99.1
// Rust in reverse: no std cookie type - frameworks own it; the wire format
// is a name=value pair plus attribute tokens
fn main() {
let name = "RSAPP_SID";
let value = "demo-a44";
let header = format!("{}={}; Path=/; HttpOnly", name, value);
let pair = header.split("; ").next().unwrap();
let mut parts = pair.splitn(2, '=');
let parsed_name = parts.next().unwrap();
let parsed_value = parts.next().unwrap();
println!("name: {} value: {}", parsed_name, parsed_value);
println!("directives: {}", header.split("; ").count() - 1);
}
name: RSAPP_SID value: demo-a44 directives: 2
TypeScript v26.13.1
// TS in reverse: type the cookie pair you hand to Set-Cookie - the name is
// just the first token of the header string
const header = "APP_SID=demo-a44; Path=/; HttpOnly";
const [pair] = header.split("; ");
const eq = pair.indexOf("=");
const name = pair.slice(0, eq);
const value = pair.slice(eq + 1);
console.log(`name: ${name} value: ${value}`);
console.log("directives:", header.split("; ").length - 1);
name: APP_SID value: demo-a44 directives: 2
Language differences
C: a cookie is a struct plus one printf of the Set-Cookie line (shown); the name is a string field and parsing is on you.
C++: the same struct approach as C (shown); framework cookie classes (Drogon/Crow) wrap the validation for you.
C#: System.Net.Cookie is the BCL cookie model (shown); ASP.NET auth middleware (CookieAuthenticationOptions.Cookie.Name) is where the session cookie name is actually configured.
Go: http.Cookie models it directly (shown); gorilla/sessions' CookieStore takes the name as a constructor option.
Java: the JDK has no cookie class - jakarta.servlet.Cookie lives in the container (Tomcat's default is JSESSIONID) and Spring's server.servlet.session.cookie.name is the config point (the example models it as a Map).
JavaScript: express-session's name option (default connect.sid); writing a raw response, the first segment of the Set-Cookie header is the name (shown).
PHP: session_name reads/writes the SESSION COOKIE NAME (default PHPSESSID): it must run before session_start (7.2+ checks session status), and changing it after the cookie is out raises E_WARNING; when setting, it returns the OLD name - the return semantics are the opposite of intuition (the example walks the whole read-default -> rename -> read-new -> restore chain). Division of labor with setcookie: session_name only owns the session cookie's name; the other attributes (path/domain/lifetime) belong to session_set_cookie_params.
Python: Flask's SESSION_COOKIE_NAME setting (default session); the stdlib http.cookies.SimpleCookie is the cookie object model (shown).
Rust: no std cookie type - axum/actix-web's cookie crate (Cookie::with_name) owns it; on the wire it is just a name=value; attributes string (shown).
TypeScript: same as JS - the Set-Cookie header string plus framework options; TS types the parsed result (shown).