How can the PHP script be modified to handle language selection based on the flag images?

To handle language selection based on flag images, we can modify the PHP script to pass a language parameter in the URL when a flag image is clicked. This parameter can then be used to set the language session variable and display content in the selected language.

```php
<?php
session_start();

if(isset($_GET['lang'])) {
    $_SESSION['lang'] = $_GET['lang'];
}

if(isset($_SESSION['lang'])) {
    include 'languages/'.$_SESSION['lang'].'.php';
} else {
    include 'languages/english.php';
}

// Display content in the selected language
echo $lang['welcome_message'];
```

In this code snippet, when a flag image is clicked, it appends a language parameter to the URL (e.g., ?lang=es for Spanish). The script then sets the language session variable based on the selected language and includes the corresponding language file to display content in that language.