Issues with HTML select

Asked by At

I wrote code which checks the user's language and opens a file (if the user's language is English it opens en.php and so on). I added an option that can change your language in settings, but this isn't working.

# Checks for user's language
if (isset($_COOKIE['lang'])) {
   $lang = substr(explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE'])[0],0,2)?:'en';
   if ($lang = $_COOKIE['lang']){
         $lang = $_COOKIE['lang'];
         setcookie("lang",$lang,time()+3650*24*60*60);
         $_SESSION['lang'] = $lang;
     } else {
       # $lang = substr(explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE'])[0],0,2)?:'en';
        setcookie("lang",$lang,time()+3650*24*60*60);
        $_SESSION['lang'] = $lang;
     }
   }

if ($lang === "en") {
        require 'translations/en.php'; # ENGLISH
     } # also every other language is included in elseif 
<!-- The form goes to that same page but at the beginning there are files included -->
<form method="post" action="settings.php" id="form2" name="form2">
<select class="settingsselect" name="language_select">
<option disabled selected>Select a Language</option>
<option value="something">some language</option>
</select>
</form>
<button class="updatesettings" type="submit" id="updatesettings" name="updatesettings" form="form2">Update Settings</button>
# Update code
if (isset($_POST['updatesettings'])) {
    $lang = $_POST['language_select'];
    setcookie("lang",$lang,time()+3650*24*60*60);
    $_SESSION['lang'] = $lang;
}
#files included in the beginning

require 'language_change.php';
require 'password_check.php';
require 'translation.php';

1 Answers

0
user3584233 On

Some examples of php files you have provided are approximate and full of syntax errors, it probably won't work because your approach is wrong, try changing the logic of your php... and since we can't guess what's wrong (your code should be rewritten from scratch), I created a php example of how it should work, please don't copy it but take inspiration from it, thanks.

index.php

<?php
  @ini_set('default_charset', 'UTF-8');
  header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  header("Cache-Control: no-store, no-cache, must-revalidate");
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Pragma: no-cache");
?>
<!DOCTYPE html>

<html lang="<?php echo isset($_COOKIE['language']) ? htmlspecialchars($_COOKIE['language'], ENT_QUOTES, 'UTF-8') : (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) : 'en'); ?>">

<head>
  <meta charset="UTF-8">
  <meta name="robots" content="noindex, nofollow">
  <title>Change Language</title>
<?php
  function check_https() {
    $isSecure = false;
    if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
      $isSecure = true;
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && strtolower($_SERVER['HTTP_X_FORWARDED_SSL']) == 'on') {
      $isSecure = true;
    }
    return $isSecure;
  }
  $check_https = check_https() ? 'https' : 'http';
  if (isset($_POST['change'])) {
    switch ($_POST['language']) {
      case 'en':
       if ($check_https == 'http') {
          setcookie('language', 'en', time()+31556926 , '/', NULL, NULL, TRUE);
        } else {
          setcookie('language', 'en', time()+31556926 , '/', NULL, TRUE, TRUE);
        }
        $_COOKIE['language'] = 'en';
      break;
      case 'it':
        if ($check_https == 'http') {
          setcookie('language', 'it', time()+31556926 , '/', NULL, NULL, TRUE);
        } else {
          setcookie('language', 'it', time()+31556926 , '/', NULL, TRUE, TRUE);
        }
        $_COOKIE['language'] = 'it';
      break;
      case 'fr':
        if ($check_https == 'http') {
          setcookie('language', 'fr', time()+31556926 , '/', NULL, NULL, TRUE);
        } else {
          setcookie('language', 'fr', time()+31556926 , '/', NULL, TRUE, TRUE);
        }
        $_COOKIE['language'] = 'fr';
      break;
      case 'es':
        if ($check_https == 'http') {
          setcookie('language', 'es', time()+31556926 , '/', NULL, NULL, TRUE);
        } else {
          setcookie('language', 'es', time()+31556926 , '/', NULL, TRUE, TRUE);
        }
        $_COOKIE['language'] = 'es';
      break;
      default:
    }
  }
?>
  <link rel="shortcut icon" href="/favicon.ico">
  <style>
  <!--
    html {
      min-width: 360px !important;
    }
    .lang-content {
      width: 210px;
    }
  -->
  </style>
</head>

<body>

<div class="lang-content">
<fieldset><legend>Change Language</legend>
<form name="language" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<select name="language" class="lang">
<option class="my_language" value="en"<?php if (isset($_COOKIE['language']) && $_COOKIE['language'] == 'en') { echo ' selected'; } ?>>en</option>
<option class="my_language" value="it"<?php if (isset($_COOKIE['language']) && $_COOKIE['language'] == 'it') { echo ' selected'; } ?>>it</option>
<option class="my_language" value="fr"<?php if (isset($_COOKIE['language']) && $_COOKIE['language'] == 'fr') { echo ' selected'; } ?>>fr</option>
<option class="my_language" value="es"<?php if (isset($_COOKIE['language']) && $_COOKIE['language'] == 'es') { echo ' selected'; } ?>>es</option>
</select>
<input class="button" type="submit" name="change" value="Change Language">
</form>
</fieldset>
</div>

<?php
  if (isset($_COOKIE['language'])) {
    $my_language = htmlspecialchars($_COOKIE['language'], ENT_QUOTES, 'UTF-8');
    if (file_exists(realpath("$my_language.php"))) {
      include("$my_language.php");
      echo PHP_EOL;
      echo PHP_EOL;
    } else {
      include("en.php");
      echo PHP_EOL;
      echo PHP_EOL;
    }
  } else {
    $my_language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2) : 'en';
    if (file_exists(realpath("$my_language.php"))) {
      include("$my_language.php");
      echo PHP_EOL;
      echo PHP_EOL;
    } else {
      include("en.php");
      echo PHP_EOL;
      echo PHP_EOL;
    }
  }
?>
</body>

</html>

en.php

<?php
  echo "Welcome!";
?>

es.php

<?php
  echo "Bienvenida!";
?>

fr.php

<?php
  echo "Bienvenue!";
?>

it.php

<?php
  echo "Benvenuto!";
?>

Live Example 1

Live Example 2

The html generated is like this:

<!DOCTYPE html>

<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="robots" content="noindex, nofollow">
  <title>Change Language</title>
  <link rel="shortcut icon" href="/favicon.ico">
  <style>
  <!--
    html {
      min-width: 360px !important;
    }
    .lang-content {
      width: 210px;
    }
  -->
  </style>
</head>

<body>

<div class="lang-content">
<fieldset><legend>Change Language</legend>
<form name="language" action="/test/index.php" method="post">
<select name="language" class="lang">
<option class="my_language" value="en" selected>en</option>
<option class="my_language" value="it">it</option>
<option class="my_language" value="fr">fr</option>
<option class="my_language" value="es">es</option>
</select>
<input class="button" type="submit" name="change" value="Change Language">
</form>
</fieldset>
</div>

Welcome!

</body>

</html>

I hope this helps.