In PHP, I want to change the language (English, German, etc) of the site when clicking a button. Is this the right way to approach that problem?
<?php
$language;
if ($language == "en") {
include("headerEn.php");
} else {
include("header.php");
}
?>
<a href="index.php"><?php $language = "en"; ?>
<img src="images/language/languageNO.png"></a>
<a href="index.php"><?php $language = "no"; ?>
<img src="images/language/languageEN.png"></a>
What is the best way to change the language of the site and have it persist when the user returns?
You can implement the same code like this . I have editted your code .
this will solve your problem .
It is always good to have a default value, so that you never end up being in a no-language site.
And then create links like this:
You can't change a variable within PHP by html. PHP is serversided HTML is clientsided
You can use GET variables to change it though. It is the easiest way to do it.
To give a solution without changing your approach, You can do like this.
If you want to keep the selection, you can store this value in Database or Session.
Try to save this
$language
value to session variable. When the page reloaded check that the session variable is set or not.If set use that
$language
NOTE:
you can do this by
and get the languages and store them in cookie and include file according to cookie like
and than