What are best practices for handling session variables to prevent overwriting in PHP?

Session variables in PHP can be overwritten if multiple scripts try to access and modify the same session variable simultaneously. To prevent this, it is best practice to use session locks to ensure that only one script can access the session variable at a time. This can be achieved by using the `session_write_close()` function to release the session lock after writing to the session variable.

session_start();

// Lock the session to prevent other scripts from accessing it
session_write_close();

// Access and modify the session variable safely
$_SESSION['variable'] = 'value';