What are the basic principles of using sessions in PHP and how can variables be accessed across different pages?

To use sessions in PHP, you need to start the session using session_start() at the beginning of each page where you want to access session variables. Session variables can be set and accessed using the $_SESSION superglobal array. This allows you to store and retrieve data across different pages for a specific user session.

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

// Set a session variable
$_SESSION['username'] = 'JohnDoe';

// Access the session variable on another page
session_start();
echo $_SESSION['username'];
?>