This fragment of code:
<?php $string = basename(__FILE__); ?>
<?php echo "<a href=".$string.'?lang=en >'; $_SESSION['lang']= 'en'; ?><img src="images/en.png" /></a>
<?php echo "<a href=".$string.'?lang=hk >'; $_SESSION['lang']= 'hk'; ?><img src="images/hk.png" /></a>
<?php echo "<a href=".$string.'?lang=cn >'; $_SESSION['lang']= 'cn'; ?><img src="images/cn.png" /></a>
Will always set your $_SESSION['lang']
to cn
value, not only when you click certain anchor. Instead you should change it to:
<?php $string = basename(__FILE__); ?>
<a href="<?php echo $string; ?>?lang=en"><img src="images/en.png" /></a>
<a href="<?php echo $string; ?>?lang=hk"><img src="images/hk.png" /></a>
<a href="<?php echo $string; ?>?lang=cn"><img src="images/cn.png" /></a>
And also at the beginning of the file you should add:
<?php
if(!isset($_SESSION['lang']))
$_SESSION['lang'] = 'cn'; // Sets default language to 'cn'
if(isset($_GET['lang']) && in_array($_GET['lang'], array('en', 'hk', 'cn')))
$_SESSION['lang'] = $_GET['lang']; // Sets language based on URL
?>
Don't forget to add session_start()
at the beginning, before previously mentioned block of code aswell, if you don't have it yet.
From now on, if you won't have ?lang=...
in your URL, you will have your last lang set in $_SESSION['lang']
.
To change links in the other menu (<li><a href="index.php"><?php echo $lang['MENU_HOME']; ?></a></li>
and so on), you should change their href's
from:
<li><a href="filename.php"><?php echo $lang['MENU_HOME']; ?></a></li>
To:
<li><a href="filename.php?lang=<?php echo $_SESSION['lang']; ?>"><?php echo $lang['MENU_HOME']; ?></a></li>