In order to securely handle user sessions in PHP and prevent session hijacking:
1. Always use HTTPS to protect cookies from being intercepted.
2. We can set the HttpOnly and Secure flags for session cookies to prevent them from being accessed via JavaScript or over an insecure connection.
ini_set('session.cookie_secure', 1);
ini_set('session.cookie_httponly', 1);
3. We should regenerate session IDs upon login to avoid any session fixation attacks.
session_start();
session_regenerate_id(true);
4. We can set a session timeout to destroy any session after a certain period of inactivity.
ini_set('session.gc_maxlifetime', 1800);
Implementing these steps can help in minimizing the risk of session hijacking attacks.
Also, we should regularly monitor the sessions to ensure that session data is properly stored and validated.