session_status

session_status(): int

Queries the three-state session machine: PHP_SESSION_DISABLED / NONE (enabled but not started) / ACTIVE (started) — the standard guard for idempotent session_start() calls.

C C23

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

// C has no session concept: model the same state machine in memory
enum { PHP_SESSION_DISABLED = 0, PHP_SESSION_NONE = 1, PHP_SESSION_ACTIVE = 2 };

struct session { int status; char user[32]; };

void session_start(struct session *s) {
    if (s->status == PHP_SESSION_NONE) { s->status = PHP_SESSION_ACTIVE; }
}

int main(void) {
    struct session s = { PHP_SESSION_NONE, "" };
    printf("%d\n", s.status == PHP_SESSION_NONE);
    session_start(&s);
    printf("%d\n", s.status == PHP_SESSION_ACTIVE);
    strcpy(s.user, "ada");
    printf("%s\n", s.user);
    return 0;
}
Output
1
1
ada

C++ C++26

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

// C++ has no session concept: model the same state machine in memory
enum class SessionState { Disabled = 0, None = 1, Active = 2 };

class Session {
public:
    SessionState status = SessionState::None;
    std::map<std::string, std::string> data; // the in-process equivalent of $_SESSION
    void start() {
        if (status == SessionState::None) { status = SessionState::Active; }
    }
};

int main() {
    Session s;
    std::cout << (s.status == SessionState::None) << "\n";
    s.start();
    std::cout << (s.status == SessionState::Active) << "\n";
    s.data["user"] = "ada";
    std::cout << s.data["user"] << "\n";
    return 0;
}
Output
1
1
ada

C# v11.0.100

v11.0.100
// C# has no built-in session state machine: model the equivalent in memory
var session = new Session();
Console.WriteLine(session.Status == Session.None);
session.Start();
Console.WriteLine(session.Status == Session.Active);
session.Data["user"] = "ada";
Console.WriteLine(session.Data["user"]);

class Session {
    public const int Disabled = 0, None = 1, Active = 2;
    public int Status { get; set; } = None;
    public Dictionary<string, string> Data { get; } = new(); // the $_SESSION equivalent
    public void Start() { if (Status == None) { Status = Active; } }
}
Output
True
True
ada

Go v1.27.4

v1.27.4
package main

import "fmt"

// Go has no session concept: model the same state machine in memory
const (
	sessionDisabled = 0 // PHP_SESSION_DISABLED: sessions disabled at the module level
	sessionNone     = 1 // PHP_SESSION_NONE: enabled, not started
	sessionActive   = 2 // PHP_SESSION_ACTIVE: started
)

type session struct {
	status int
	data   map[string]string // the in-process equivalent of $_SESSION
}

func (s *session) start() {
	if s.status == sessionNone {
		s.status = sessionActive
	}
}

func main() {
	s := &session{status: sessionNone, data: map[string]string{}}
	fmt.Println(s.status == sessionNone)
	s.start()
	fmt.Println(s.status == sessionActive)
	s.data["user"] = "ada"
	fmt.Println(s.data["user"])
}
Output
true
true
ada

Java v27.0.1

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

public class Main {
    static final int PHP_SESSION_NONE = 1, PHP_SESSION_ACTIVE = 2;

    // Java has no built-in session: model the same state machine in memory
    static class Session {
        int status = PHP_SESSION_NONE;
        Map<String, String> data = new HashMap<>(); // the $_SESSION equivalent
        void start() { if (status == PHP_SESSION_NONE) { status = PHP_SESSION_ACTIVE; } }
    }

    public static void main(String[] args) {
        Session s = new Session();
        System.out.println(s.status == PHP_SESSION_NONE);
        s.start();
        System.out.println(s.status == PHP_SESSION_ACTIVE);
        s.data.put("user", "ada");
        System.out.println(s.data.get("user"));
    }
}
Output
true
true
ada

JavaScript v26.13.1

v26.13.1
// JavaScript has no built-in session: model the same state machine in memory
const PHP_SESSION_NONE = 1, PHP_SESSION_ACTIVE = 2;
const session = { status: PHP_SESSION_NONE, data: {} };
console.log(session.status === PHP_SESSION_NONE);
session.status = PHP_SESSION_ACTIVE; // session_start()
console.log(session.status === PHP_SESSION_ACTIVE);
session.data.user = "ada"; // $_SESSION["user"] = "ada"
console.log(session.data.user);
Output
true
true
ada

PHP v8.6.0

v8.6.0
$before = session_status();
session_start(); // no output may precede this in a real HTTP script
$after = session_status();
// CLI: sessions are enabled, so NONE -> ACTIVE across session_start()
var_dump($before === PHP_SESSION_NONE, $after === PHP_SESSION_ACTIVE);
Output
bool(true)
bool(true)

Python v3.15.2

v3.15.2
# Python has no built-in session: model the same state machine in memory
class Session:
    DISABLED, NONE, ACTIVE = 0, 1, 2  # the PHP three states

    def __init__(self):
        self.status = self.NONE
        self.data = {}  # the in-process equivalent of $_SESSION

    def start(self):
        if self.status == self.NONE:
            self.status = self.ACTIVE

s = Session()
print(s.status == Session.NONE)
s.start()
print(s.status == Session.ACTIVE)
s.data["user"] = "ada"
print(s.data["user"])
Output
True
True
ada

Rust v1.99.1

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

// Rust has no built-in session: model the same state machine in memory
const PHP_SESSION_NONE: u8 = 1;
const PHP_SESSION_ACTIVE: u8 = 2;

struct Session {
    status: u8,
    data: HashMap<String, String>, // the in-process equivalent of $_SESSION
}

impl Session {
    fn start(&mut self) {
        if self.status == PHP_SESSION_NONE {
            self.status = PHP_SESSION_ACTIVE;
        }
    }
}

fn main() {
    let mut s = Session { status: PHP_SESSION_NONE, data: HashMap::new() };
    println!("{}", s.status == PHP_SESSION_NONE);
    s.start();
    println!("{}", s.status == PHP_SESSION_ACTIVE);
    s.data.insert("user".to_string(), "ada".to_string());
    println!("{}", s.data["user"]);
}
Output
true
true
ada

TypeScript v26.13.1

v26.13.1
// TypeScript has no built-in session: model the same state machine in memory
const PHP_SESSION_NONE: number = 1, PHP_SESSION_ACTIVE: number = 2;
const session: { status: number; data: Record<string, string> } = {
  status: PHP_SESSION_NONE,
  data: {},
};
console.log(session.status === PHP_SESSION_NONE);
session.status = PHP_SESSION_ACTIVE; // session_start()
console.log(session.status === PHP_SESSION_ACTIVE);
session.data["user"] = "ada"; // $_SESSION["user"] = "ada"
console.log(session.data["user"]);
Output
true
true
ada

Language differences

PHP three states: PHP_SESSION_DISABLED(0) sessions disabled at the module level / NONE(1) enabled but not started / ACTIVE(2) started; sessions work in the CLI, and the example asserts the NONE->ACTIVE two states (with no prior output, which would make session_start fail with "headers already sent").

Idiom: session_status() === PHP_SESSION_NONE before session_start() — more reliable than isset($_SESSION) or session_id() === '' (both misreport after session_write_close()).

PHP_SESSION_DISABLED only appears when sessions are disabled at the module level (e.g. a session-less build); it cannot be demonstrated inside a normal process — noted as-is.

Other languages have no built-in session state machine: the examples model the equivalent three states in memory (NONE start, start() transitions to ACTIVE, a dict plays $_SESSION).

Real session state lives at the HTTP layer: a cookie carries the session id while files/database hold the data on the server — frameworks (Django/Express/Spring and friends) implement the equivalent machine at the framework level, not the language level.

Numeric convention: the three constants are 0/1/2 (manual user notes and php-src agree); every language example reuses the same values for comparison.