session_save_path

session_save_path(?string $path = null): string|false

Reads/writes the storage directory of the built-in files handler; setting must happen before session_start(), and the setter returns the previous path (false when none was set). The storage-location switch short of session_set_save_handler.

C C23

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

int main(void) {
    /* storage location is configuration: create a per-run directory, write, verify, clean up */
    const char *td = getenv("TMPDIR");
    if (!td || td[0] == '\0') td = ".";
    char tmpl[256];
    snprintf(tmpl, sizeof tmpl, "%s/codeabc-XXXXXX", td);
    char *dir = mkdtemp(tmpl);
    printf("path set: %s\n", dir ? "yes" : "no");
    char file[300];
    snprintf(file, sizeof file, "%s/sess_demo", dir);
    FILE *fp = fopen(file, "w");
    fputs("k|v", fp);
    fclose(fp);
    printf("written: %s\n", access(file, F_OK) == 0 ? "yes" : "no");
    char buf[8] = {0};
    fp = fopen(file, "r");
    fgets(buf, sizeof buf, fp);
    fclose(fp);
    printf("visible: %s\n", strcmp(buf, "k|v") == 0 ? "yes" : "no");
    unlink(file);
    rmdir(dir);
    return 0;
}
Output
path set: yes
written: yes
visible: yes

C++ C++26

C++26
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <unistd.h>

int main() {
    // storage location is configuration: per-run directory, write, verify, clean up
    const char *td = std::getenv("TMPDIR");
    const std::string base = (td && td[0]) ? td : ".";
    char tmpl[256];
    std::snprintf(tmpl, sizeof tmpl, "%s/codeabc-XXXXXX", base.c_str());
    char *made = mkdtemp(tmpl);
    const std::string dir = made ? made : "";
    std::cout << "path set: " << (dir.empty() ? "no" : "yes") << "\n";
    const std::string file = dir + "/sess_demo";
    { std::ofstream out(file); out << "k|v"; }
    std::ifstream in(file);
    std::string content((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
    std::cout << "written: yes\n";
    std::cout << "visible: " << (content == "k|v" ? "yes" : "no") << "\n";
    unlink(file.c_str());
    rmdir(dir.c_str());
    return 0;
}
Output
path set: yes
written: yes
visible: yes

C# v11.0.100

v11.0.100
// storage location is configuration: a per-run directory under the system temp path
string dir = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "codeabc-" + System.Guid.NewGuid().ToString("N")[..12]);
System.IO.Directory.CreateDirectory(dir);
Console.WriteLine("path set: yes");
string file = System.IO.Path.Combine(dir, "sess_demo");
System.IO.File.WriteAllText(file, "k|v");
Console.WriteLine("written: " + (System.IO.File.Exists(file) ? "yes" : "no"));
string back = System.IO.File.ReadAllText(file);
Console.WriteLine("visible: " + (back == "k|v" ? "yes" : "no"));
System.IO.File.Delete(file);
System.IO.Directory.Delete(dir);
Output
path set: yes
written: yes
visible: yes

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	// storage location is configuration: a per-run temp dir, write, verify, clean up
	dir, err := os.MkdirTemp("", "codeabc-")
	if err != nil {
		panic(err)
	}
	fmt.Println("path set: yes")
	file := filepath.Join(dir, "sess_demo")
	os.WriteFile(file, []byte("k|v"), 0600)
	if _, err := os.Stat(file); err == nil {
		fmt.Println("written: yes")
	}
	data, _ := os.ReadFile(file)
	fmt.Println("visible:", map[bool]string{true: "yes", false: "no"}[string(data) == "k|v"])
	os.Remove(file)
	os.Remove(dir)
}
Output
path set: yes
written: yes
visible: yes

Java v27.0.1

v27.0.1
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws IOException {
        // storage location is configuration: a per-run temp dir, write, verify, clean up
        Path dir = Files.createTempDirectory("codeabc-");
        System.out.println("path set: yes");
        Path file = dir.resolve("sess_demo");
        Files.writeString(file, "k|v");
        System.out.println("written: " + (Files.exists(file) ? "yes" : "no"));
        String back = Files.readString(file);
        System.out.println("visible: " + (back.equals("k|v") ? "yes" : "no"));
        Files.delete(file);
        Files.delete(dir);
    }
}
Output
path set: yes
written: yes
visible: yes

JavaScript v26.13.1

v26.13.1
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");

// storage location is configuration: a per-run temp dir, write, verify, clean up
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "codeabc-"));
console.log("path set: yes");
const file = path.join(dir, "sess_demo");
fs.writeFileSync(file, "k|v");
console.log("written:", fs.existsSync(file) ? "yes" : "no");
const back = fs.readFileSync(file, "utf8");
console.log("visible:", back === "k|v" ? "yes" : "no");
fs.rmSync(dir, { recursive: true });
Output
path set: yes
written: yes
visible: yes

PHP v8.6.0

v8.6.0
// session_save_path: get/set the storage directory of the built-in files save handler.
// Setting it must happen BEFORE session_start(). The setter returns the previous path (or false if none was set).
$tmp = sys_get_temp_dir() . "/codeabc-a45-store";
if (!is_dir($tmp)) { mkdir($tmp, 0700, true); }
$prev = session_save_path($tmp);
$now = session_save_path();
$match = $now === $tmp;
session_start();
$_SESSION["k"] = "v";
session_write_close();
$stored = glob($tmp . "/sess_*") !== [];
session_start();          // reopen from the SAME directory proves persistence
$same = ($_SESSION["k"] ?? "") === "v";
session_destroy();
foreach (glob($tmp . "/sess_*") as $f) { unlink($f); }
rmdir($tmp);
echo "prev type: ", $prev === false ? "false" : "string", "\n";
echo "now matches: ", var_export($match, true), "\n";
echo "file stored: ", var_export($stored, true), "\n";
echo "reload ok: ", var_export($same, true);
Output
prev type: string
now matches: true
file stored: true
reload ok: true

Python v3.15.2

v3.15.2
import os
import tempfile

# storage location is configuration: a per-run temp dir, write, verify, clean up
# (the session-framework counterpart is a store path/URL in settings, e.g. Redis URL)
dir = tempfile.mkdtemp(prefix="codeabc-")
print("path set: yes")
file = os.path.join(dir, "sess_demo")
with open(file, "w") as fp:
    fp.write("k|v")
print("written: yes" if os.path.exists(file) else "written: no")
with open(file) as fp:
    back = fp.read()
print("visible: yes" if back == "k|v" else "visible: no")
for name in os.listdir(dir):
    os.unlink(os.path.join(dir, name))
os.rmdir(dir)
Output
path set: yes
written: yes
visible: yes

Rust v1.99.1

v1.99.1
use std::fs;
use std::path::PathBuf;

fn main() {
    // storage location is configuration: a per-run dir under the system temp path
    let dir: PathBuf = std::env::temp_dir().join("codeabc-save-path-demo");
    let _ = fs::remove_dir_all(&dir);
    fs::create_dir_all(&dir).unwrap();
    println!("path set: yes");
    let file = dir.join("sess_demo");
    fs::write(&file, "k|v").unwrap();
    println!("written: {}", if file.exists() { "yes" } else { "no" });
    let back = fs::read_to_string(&file).unwrap();
    println!("visible: {}", if back == "k|v" { "yes" } else { "no" });
    fs::remove_dir_all(&dir).unwrap();
}
Output
path set: yes
written: yes
visible: yes

TypeScript v26.13.1

v26.13.1
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";

// storage location is configuration: a per-run temp dir, write, verify, clean up
const dir: string = fs.mkdtempSync(path.join(os.tmpdir(), "codeabc-"));
console.log("path set: yes");
const file: string = path.join(dir, "sess_demo");
fs.writeFileSync(file, "k|v");
console.log("written:", fs.existsSync(file) ? "yes" : "no");
const back: string = fs.readFileSync(file, "utf8");
console.log("visible:", back === "k|v" ? "yes" : "no");
fs.rmSync(dir, { recursive: true });
Output
path set: yes
written: yes
visible: yes

Language differences

C: the storage location is just a config item of the hand-rolled framework (config file/env vars); a file-backed session store means one file per session plus a filesystem choice that handles many small files well.

C++: same as C; std::filesystem covers path and permission handling.

C#: the framework layer has no "storage path" — ASP.NET in-process state or a distributed store (Redis connection string); the location is configuration (appsettings.json).

Go: at the language level os.MkdirTemp/os.TempDir; gorilla/sessions' file store takes the directory explicitly (NewFilesystemStore(path)).

Java: the container owns session storage location (Tomcat's work directory etc.); an app-layer Redis setup only has a connection string.

JavaScript: at the language level os.tmpdir + mkdtemp; express-session's FileStore takes a directory argument, production swaps in Redis/Mongo (the connection string is the "path").

PHP: session_save_path only drives the built-in files handler (once session_set_save_handler steps in, a custom store replaces it); it must be set before session_start(); it supports N;path depth notation (e.g. "2;/tmp/sessions" sharding by the id's first two chars to avoid single-directory file explosions); the directory must be writable by the web user, or GC cannot delete other users' session files. CLI demos should always point at a temp directory, never write the system session dir.

Python: the storage location is framework configuration — Django SESSION_FILE_PATH or a redis:// URL; at the language level tempfile is the closest directory tool.

Rust: at the language level std::env::temp_dir; framework layers take the location in the session store constructor (e.g. a sqlx-sessions connection string).

TypeScript: same as JavaScript.