session_start(); if (session_exists()) { // do something if session exists } else { // do something else if session does not exist }
session_start(); if ($_SESSION['user_id']) { // do something if user is logged in } else { // redirect to login page if user is not logged in header('Location: login.php'); exit(); }In both examples, the `session_start()` function is used to initiate a session. The `session_exists()` function (which is not a built-in PHP function) is used in example 1 to check if a session exists. In example 2, the `$_SESSION` superglobal is used to access session data and check if a user is logged in. The package library used in these examples is the PHP standard library, as sessions are a built-in feature of PHP.