How can PHP developers prevent session hijacking and ensure secure session management when using a database for session storage?

To prevent session hijacking and ensure secure session management when using a database for session storage, PHP developers can implement the following measures: 1. Use HTTPS to encrypt data transmission between the client and server. 2. Generate unique session IDs using a strong algorithm and store them securely. 3. Implement session regeneration to change the session ID periodically.

// Start a secure session
session_start();

// Set session cookie parameters
$cookieParams = session_get_cookie_params();
$cookieParams['secure'] = true; // Ensures session cookie is only sent over HTTPS
$cookieParams['httponly'] = true; // Prevents session cookie from being accessed through JavaScript

session_set_cookie_params($cookieParams['lifetime'], $cookieParams['path'], $cookieParams['domain'], $cookieParams['secure'], $cookieParams['httponly']);

// Regenerate session ID periodically
if (mt_rand(1, 100) == 1) {
    session_regenerate_id(true);
}