How can you properly log out a user in PHP and ensure their session is ended securely?

To properly log out a user in PHP and ensure their session is ended securely, you should destroy the session data and regenerate the session ID to prevent session fixation attacks. This can be achieved by calling session_unset() to clear all session variables and session_destroy() to destroy the session. Finally, call session_regenerate_id(true) to generate a new session ID.

session_start();
session_unset();
session_destroy();
session_regenerate_id(true);