What are some common issues when using $_GET and sessions in PHP for language selection on a bilingual website?

One common issue when using $_GET and sessions for language selection on a bilingual website is that the language selection may not persist across different pages. To solve this, you can store the selected language in a session variable and check for it on each page load to set the language accordingly.

<?php
session_start();

// Check if the language is selected via $_GET
if(isset($_GET['lang'])) {
    $_SESSION['lang'] = $_GET['lang'];
}

// Set the default language if none is selected
if(!isset($_SESSION['lang'])) {
    $_SESSION['lang'] = 'en'; // default language is English
}

// Include language files based on the selected language
if($_SESSION['lang'] == 'fr') {
    include 'lang_fr.php';
} else {
    include 'lang_en.php';
}
?>