session_regenerate_id
session_regenerate_id(bool $delete_old_session = false): bool
Replaces the current session ID with a new one while keeping session data — the standard move in session fixation defense.
C C23
#include <stdio.h>
#include <string.h>
/* C has no session layer; a fixation-defense sketch shows ID rotation over
a server-side store keyed by the session ID (deterministic LCG for demo) */
static void make_id(char *out) {
static unsigned long seed = 12345u;
for (int i = 0; i < 32; i++) {
seed = seed * 1103515245u + 12345u;
out[i] = "0123456789abcdef"[(seed >> 16) & 0xF];
}
out[32] = '\0';
}
int main(void) {
char old_id[33], fresh[33];
make_id(old_id);
make_id(fresh); /* rotation on privilege change (fixation defense) */
printf("%s\n", strcmp(old_id, fresh) != 0 ? "rotated" : "same");
printf("%s\n", strlen(fresh) == 32 ? "format ok" : "bad");
return 0;
}
rotated format ok
C++ C++26
#include <iostream>
#include <string>
// C++ has no session layer; the ID-rotation mechanics sketched with a
// deterministic generator (real apps use crypto RNG + framework sessions)
static std::string make_id() {
static unsigned long seed = 12345u;
std::string id;
for (int i = 0; i < 32; i++) {
seed = seed * 1103515245u + 12345u;
id += "0123456789abcdef"[(seed >> 16) & 0xF];
}
return id;
}
int main() {
std::string old_id = make_id();
std::string fresh = make_id(); // rotation on privilege change
std::cout << (old_id != fresh ? "rotated" : "same") << "\n";
std::cout << (fresh.size() == 32 ? "format ok" : "bad") << "\n";
return 0;
}
rotated format ok
C# v11.0.100
// ASP.NET Core: session ID rotation is framework-level (ISession is a
// key-value bag); the ID-generation sketch uses Guid.NewGuid
using System.Collections.Generic;
var old = System.Guid.NewGuid().ToString();
var store = new Dictionary<string, string> { ["user"] = "alice" };
var fresh = System.Guid.NewGuid().ToString(); // rotation on privilege change
Console.WriteLine(old != fresh);
Console.WriteLine(store["user"] == "alice");
True True
Go v1.27.4
package main
import (
"crypto/rand"
"encoding/hex"
"fmt"
)
// Go has no session machinery in the stdlib (gorilla/sessions etc. own ID
// rotation); the ID generation itself is shown here
func newSID() string {
b := make([]byte, 16)
_, _ = rand.Read(b)
return hex.EncodeToString(b)
}
func main() {
old := newSID()
fresh := newSID() // rotation on privilege change (fixation defense)
fmt.Println(old != fresh)
fmt.Println(len(fresh) == 32)
}
true true
Java v27.0.1
// Servlet has the native analogue: HttpServletRequest#changeSessionId()
// (container-level, keeps session data). Standalone demo sketches the
// rotate-keep-data mechanics:
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
class Main {
public static void main(String[] args) {
Map<String, String> store = new HashMap<>();
String old = UUID.randomUUID().toString();
store.put("user", "alice");
String fresh = UUID.randomUUID().toString(); // changeSessionId() analogue
System.out.println(!old.equals(fresh));
System.out.println(store.get("user").equals("alice"));
}
}
true true
JavaScript v26.13.1
// express-session exposes req.session.regenerate() (framework level);
// the ID-rotation mechanics sketched with crypto.randomUUID
const crypto = require("node:crypto");
let sid = crypto.randomUUID();
const before = sid;
sid = crypto.randomUUID(); // rotation on privilege change (fixation defense)
console.log(before !== sid);
console.log(typeof sid === "string" && sid.length === 36);
true true
PHP v8.6.0
// Security teaching point: regenerate the ID on login/privilege change to
// defeat session fixation. ID values are random - only boolean facts are
// asserted. All session work happens BEFORE the first echo (any output
// sends headers in CLI, which would make session_start fail)
ini_set("session.save_path", sys_get_temp_dir());
session_start();
$_SESSION["user"] = "alice";
$before = session_id();
$ok = session_regenerate_id(true); // true: also delete the old session file
$after = session_id();
$changed = $before !== $after;
session_write_close();
echo "regenerated: ", $ok ? "yes" : "no", "\n";
echo "id changed: ", $changed ? "yes" : "no", "\n";
echo "data kept: ", $_SESSION["user"] === "alice" ? "yes" : "no", "\n";
regenerated: yes id changed: yes data kept: yes
Python v3.15.2
# Python's stdlib has no session concept - Flask/Django own ID rotation at
# the framework level (itsdangerous-signed cookie / session engine); the
# rotation mechanics sketched with secrets
import secrets
sid = secrets.token_hex(16) # initial session ID
old = sid
sid = secrets.token_hex(16) # rotation on privilege change (fixation defense)
print(old != sid)
print(len(sid) == 32)
True True
Rust v1.99.1
// Rust has no session machinery in the standard library; web frameworks
// (actix-session, axum) own ID rotation. ID generation via the rand crate:
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();
let old: u128 = rng.gen();
let fresh: u128 = rng.gen(); // rotation on privilege change (fixation defense)
println!("{}", old != fresh);
}
true
TypeScript v26.13.1
// Same runtime as JS: framework-level regenerate(); crypto.randomUUID
// sketch with typed helpers
const newSid = (): string => crypto.randomUUID();
let current = newSid();
const before = current;
current = newSid(); // rotation on privilege change (fixation defense)
console.log(before !== current);
console.log(current.length === 36);
true true
Language differences
C: no session layer — HTTP sessions are an application-protocol concept; in C you implement cookie parsing plus server-side storage yourself; ID rotation is essentially "swap the storage key" (the example uses a deterministic generator; real systems must use a crypto RNG).
C++: same as C, no session layer; frameworks (Drogon session middleware etc.) provide rotation.
C#: ASP.NET Core's ISession is a key-value bag with ID management at the framework level; classic ASP.NET additionally exposes SessionIDManager for explicit rotation (RegenerateSessionId).
Go: no session machinery in the stdlib — gorilla/sessions and friends own it; the example shows ID generation with crypto/rand (real rotation happens inside frameworks).
Java: Servlet has the native analogue — HttpServletRequest#changeSessionId() (Servlet 3.1+), container-level rotation that keeps data, semantically the closest to PHP.
JavaScript: the framework layer has the native analogue — express-session's req.session.regenerate() (noted in the example); the bare language only has ID-generation primitives.
PHP: the standard move in session fixation defense — at login/privilege escalation an attacker may already have planted a known ID on the victim; regenerating invalidates it; session_regenerate_id(true) also deletes the old session file, but the manual warns that deleting old data immediately can lose sessions on mobile networks — production prefers a grace period with dual IDs; the CLI demo asserts boolean facts only (ID values are random and cannot be pre-set).
Python: Flask/Django handle it at the framework level (Django 2.1+ provides session.cycle_key(), semantically identical to PHP); the stdlib has no session concept.
Rust: no session machinery in the standard library — actix-session/axum own rotation at the framework level; the language layer only has random primitives (example).
TypeScript: runtime identical to JavaScript (express-session regenerate()); types annotate the payload.
— General conclusion for this entry: "regenerate the session ID after login" is a Web security convention, not a language feature — the difference between languages is only whether your framework has baked the convention in for you.