عبد العزيز حسن عبد العزيز حسن - 4 months ago 14
PHP Question

PHP multi language issue

I have some code to change my language depending on session

Here is my php code

function make_lang(){
session_start();
if(isset($_POST['a'])){
$_SESSION['a']=true;
unset ($_SESSION['e']);
unset ($_SESSION['f']);
}
if(isset($_POST['e'])){
$_SESSION['e']=true;
unset ($_SESSION['a']);
unset ($_SESSION['f']);
}
if(isset($_POST['f'])){
$_SESSION['f']=true;
unset ($_SESSION['e']);
unset ($_SESSION['a']);
}
}

function lang_path(){

if(!isset($_SESSION['e']) or !isset($_SESSION['f'])){
include('lan/a.php');
echo '<link rel="stylesheet" type="text/css" href="css/style-z.php" />';
}

if(isset($_SESSION['a'])){
$lang = "a";
}

if(isset($_SESSION['e'])){
$lang = "e";
}

if(isset($_SESSION['f'])){
$lang = "f";
}

$path="lan/".$lang.".php";

return $path;
}

function lang_css(){
if(isset($_SESSION['a'])){
$css = "style-z";
}

if(isset($_SESSION['e'])){
$css = "style-zleft";
}

if(isset($_SESSION['f'])){
$css = "style-zleft";
}
$css_i="css/".$css.".css";
return $css_i;
}

make_lang();

$l_file = lang_path();
include($l_file);

$l_css = lang_css();
echo '<link rel="stylesheet" type="text/css" href="'.$l_css.'" />';


html form

<form method="post" class="login-img">
<input name="e" type="submit" value="English" class="login-img" />
<input name="a" type="submit" value="Arabic" class="login-img" />
<input name="f" type="submit" value="Frinch" class="login-img" />
</form>


The problem is

When you click on any hyperlink on the site does not work the code, but in the case of pressing a button language site appears well
But in the case of pressure on any other hyperlink does not work well site

Web site link: http://www.hh4track.com/

Thanks for helping.

Answer

Looking at your page source code, it looks like you are echoing style sheets outside of the <head></head> tags, and before the <!DOCTYPE ...> tag -
index page source code -

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="tracker" />
<meta name="description" content="" />
<title>HH4track</title>
<link href="css/style-z.css" rel="stylesheet" type="text/css" />
</head>

all other pages source code -

<link rel="stylesheet" type="text/css" href="css/style-z.php" /><link rel="stylesheet" type="text/css" href="css/.css" /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
....
</head>

No matter what language link was click, your echo'ed style sheets are -

<link rel="stylesheet" type="text/css" href="css/style-z.php" />
<link rel="stylesheet" type="text/css" href="css/.css" />

It does not look like your language sessions are setting correctly. Why also have 3 $_SESSION variables? You could do it with 1 - $_SESSION['lang'] and simplify your code -

session_start();
function make_lang(){
  if(isset($_POST['a'])){
    $_SESSION['lang']='a';}
  if(isset($_POST['e'])){
    $_SESSION['lang']='e';}
  if(isset($_POST['f'])){
    $_SESSION['lang']='f';}
 }

 function lang_path(){
    if($_SESSION['lang'] == 'a'){
       $lang = "a";}
    if($_SESSION['lang'] == 'e'){
       $lang = "e";}
    if($_SESSION['lang'] == 'f'){
       $lang = "f";}

    $path="lan/".$lang.".php";

    return $path;
 }

 function lang_css(){    
   if($_SESSION['lang'] == 'a'){
      $css = "style-z";}
   else{
      $css = "style-zleft";}

 $css_i="css/".$css.".css";
 return $css_i;
 }


 make_lang();

 $l_file = lang_path();
 include($l_file);

 $l_css = lang_css();
 echo '<link rel="stylesheet" type="text/css" href="'.$l_css.'" />';