What are the potential pitfalls of storing session variables in cookies in PHP, and what alternative methods can be used for secure data storage?

Storing session variables in cookies in PHP can pose a security risk as cookies are easily accessible and modifiable by users. An alternative method for secure data storage is to use PHP's built-in session handling functions to store session variables on the server side.

<?php
// Start the session
session_start();

// Store session variables securely on the server side
$_SESSION['username'] = 'example_user';
$_SESSION['email'] = 'example@example.com';

// Retrieve session variables
$username = $_SESSION['username'];
$email = $_SESSION['email'];

// Destroy the session when done
session_destroy();
?>