I wrote a simple login form website with change language in PHP. When I run in localhost, I can login after I fill in the registration number and password. I can also change the language to Chinese/English after I click on the hyperlink.
However, after I deploy the PHP files to server, the functions are not working. After I click on the English hyperlink, the login page will not change to English and remains as the default language (Chinese). The login form is not working also, I have to click on the any of the Chinese/English hyperlink and fill in the login form, then I can login to the dashboard.
After login, the dashboard language will show in the language I selected in the index.php. I have no idea on why the index.php will not change to the language I wanted. It is working fine in localhost.
Below are the codes. I appreciate if anyone can advise me on this. Thanks a lot.
index.php
<?php
session_start();
include 'includes/language.php';
?>
<html>
<div class="container2" style="justify-content: center;">
<a href="change-Language.php?language=en">
<?php echo getLanguage($resultAll, 'nav-english'); ?>
</a> |
<a href="change-Language.php?language=zh">
<?php echo getLanguage($resultAll, 'nav-chinese'); ?>
</a>
</div>
<div class="container">
<label for="regno">
<b><?php echo getLanguage($resultAll, 'nav-regno'); ?></b>
</label>
<input type="text" placeholder="<?php echo getLanguage($resultAll, 'nav-regno'); ?>" name="regno">
<label for="psw">
<b><?php echo getLanguage($resultAll, 'nav-password'); ?></b>
</label>
<input type="password" placeholder="<?php echo getLanguage($resultAll, 'nav-password'); ?>" name="password">
<button type="submit">
<?php echo getLanguage($resultAll, 'nav-login'); ?>
</button>
</div>
</html>
includes/language.php
<?php
if (!isset($_SESSION['language']))
{
$_SESSION['language'] = "zh";
}
if ( isset($_SESSION['language']) && $_SESSION['language'] == "en")
{
$query = $pdo->prepare("SELECT message_code, message_en as msg FROM tbl_language");
}
if ( isset($_SESSION['language']) && $_SESSION['language'] == "zh")
{
$query = $pdo->prepare("SELECT message_code, message_zh as msg FROM tbl_language");
}
$results = $query->execute();
$resultAll = $query->fetchAll(\PDO::FETCH_ASSOC);
function getLanguage($resultAll, $key)
{
foreach ($resultAll as $row)
{
if ($row['message_code'] == $key)
{
echo $row['msg'];
}
}
}
?>
change-Language.php
<?php
session_start();
$language = $_GET['language'];
$_SESSION['language'] = $language;
$referer = $_SERVER['HTTP_REFERER'];
header("Location: $referer");
?>