0

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");
?>
karl233
  • 1
  • 1
  • Are you sure that it really works on localhost without any problem? I recommend to turn on and [display all errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) on your localhost which is development environment and get the [error log](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php-5-apache-fastcgi-and-cpanel) on your real website which is production environment. And then maybe update your question if you found any errors. – vee Sep 09 '22 at 02:35
  • You may need to check that your real website is using [cache](https://stackoverflow.com/questions/4393626/how-do-i-know-if-any-php-caching-is-enabled) or not. This included [OPcache](https://stackoverflow.com/questions/22773041/how-to-determine-if-php-opcache-is-enabled-or-not), [memcache(d)](https://stackoverflow.com/questions/1463670/how-to-check-if-memcache-or-memcached-is-installed-for-php). Try to disable them if you found it is enabled and see if your code work. – vee Sep 09 '22 at 02:47
  • @vee yes, the switch language thing can change the language in localhost but I realized the HTTP status code is 302 after I click on the "English" in both localhost and server – karl233 Sep 09 '22 at 03:12
  • And error message? And cache? – vee Sep 09 '22 at 03:21
  • I check from the network header it just shows Status Code: 302 Found, not sure if this is the cause, I just check the website is not cache enabled @vee – karl233 Sep 09 '22 at 03:47
  • You have the header command which is doing the redirection – Rohit Gupta Sep 09 '22 at 04:37
  • By default, if you did not set HTTP status code and with `location:` header it will be returns 302 redirect. Read [their document](https://www.php.net/manual/en/function.header.php) for reference. You may need to see all useful HTTP status codes [here](https://www.restapitutorial.com/httpstatuscodes.html). – vee Sep 09 '22 at 05:20
  • Do you check for errors? I comment about this 3rd time now. Please check the errors because it is important. To debug more, inside your `if (!isset($_SESSION['language'])) {...}` in **includes/language.php** file, please add `echo 'something';` to see that it is always go into this condition (even clicked on change language) or not. – vee Sep 09 '22 at 05:23
  • Using HTTP_REFERER to try and redirect back, is a very bad idea. That value might not be set at all, if the user has some privacy-enhancing extensions running, or similar. – CBroe Sep 09 '22 at 06:32

0 Answers0