What is the purpose of using a language switch (Sprachweiche) in PHP for redirecting users to an external domain?
When redirecting users to an external domain based on their language preference, a language switch (Sprachweiche) in PHP can be used to dynamically generate the correct URL for the redirection. By detecting the user's language and then constructing the appropriate URL for the external domain, the language switch ensures that users are directed to the correct version of the external site.
<?php
// Define an array mapping languages to their respective external domains
$languages = array(
'en' => 'https://www.example.com/en',
'de' => 'https://www.example.com/de',
// Add more languages as needed
);
// Get the user's preferred language
$user_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
// Check if the user's language is supported
if (array_key_exists($user_language, $languages)) {
// Redirect the user to the correct external domain
header('Location: ' . $languages[$user_language]);
exit;
} else {
// Redirect to a default domain if the user's language is not supported
header('Location: https://www.example.com');
exit;
}
?>