When I change the language on the page everything changes and starts from the main page (“index.php?lang=en”) but the question is, when I am in “about.php” I want to stay on the same page “about.php?lang=en”…. who can help me to realize that?
session_start(); if (!isset($_SESSION['lang'])) $_SESSION['lang'] = 'en'; else if (isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang'])) { if ($_GET['lang'] == 'en') $_SESSION['lang'] = 'en'; else if ($_GET['lang'] == 'ru') $_SESSION['lang'] = 'ru'; } require_once "languages/" . $_SESSION['lang'] . ".php";
<a href="index.php?lang=en" class="dropdown-item _chng-link">english</a>
<a href="index.php?lang=ru" class="dropdown-item _chng-link">russian</a>
Advertisement
Answer
This will give you a good direction and cleaner readable & more secure flow:
<?php session_start(); $lang = 'en'; // default value $supportedLanguage = array( 'en' => 'English', 'ru' => 'Russian' ); $lang = (!empty($_SESSION['lang']) && isset($supportedLanguage[$_SESSION['lang']])) ? $_SESSION['lang'] : $lang; $lang = (!empty($_GET['lang']) && isset($supportedLanguage[$_GET['lang']])) ? $_GET['lang'] : $lang; $_SESSION['lang'] = $lang; require_once ("languages/" . $_SESSION['lang'] . ".php"); $languageLinks = ''; foreach ($supportedLanguage as $key => $value) { $URL = htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES) . '?lang='.$key; $languageLinks .= '<a href="'.$URL.'" class="dropdown-item _chng-link">'.$value.'</a> '; }