Skip to main content
Unlisted page
This page is unlisted. Search engines will not index it, and only users having a direct link can access it.

PHP Sessions

Starting a Session

session_start();
$_SESSION["user_id"] = 123;
$_SESSION["username"] = "alice";

Accessing Session Data

session_start();
if (isset($_SESSION["username"])) {
echo "Welcome back, " . $_SESSION["username"];
}

Destroying a Session

session_start();
session_destroy(); // End session

Security Best Practices

  • Regenerate session ID after login: session_regenerate_id()
  • Use HTTPS in production
  • Set appropriate session timeout