What are the potential pitfalls of storing objects in PHP sessions?

Storing complex objects in PHP sessions can lead to serialization issues, increased memory usage, and potential security vulnerabilities. To avoid these pitfalls, it's recommended to only store primitive data types in sessions and serialize/deserialize objects when needed.

// Storing only primitive data types in sessions
$_SESSION['user_id'] = 123;
$_SESSION['username'] = 'john_doe';

// Serialize/deserialize objects when needed
$user = new User();
$_SESSION['serialized_user'] = serialize($user);

// Retrieve and unserialize the object
$serialized_user = $_SESSION['serialized_user'];
$user = unserialize($serialized_user);