tags:

views:

66

answers:

3

The code works when I declare $lang like this (at the top of the index file):

index.php (with php variable declaration at the top):

   <?php
        $lang = 'es';
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
        <title>New Project</title>
        <link rel="stylesheet" type="text/css" href="styles/global.css" />
    </head>
    <body id="home">
    <div id="header">
        <div class="container">
            <div id="header-top">
                <h1><a href="http://widerdesign.co.nr/"&gt;wider design</a></h1>
                <ul id="lang">
                    <li><a href="index.php?lang=en" <?php if($lang=='en') {echo 'class="current"';} ?>>English</a></li>
                    <li><a href="index.php?lang=es" <?php if($lang=='es') {echo 'class="current"';} ?>>Español</a></li>
                    <li><a href="index.php?lang=tw" <?php if($lang=='tw') {echo 'class="current"';} ?>>中文(繁體)</a></li>
                    <li><a href="index.php?lang=cn" <?php if($lang=='cn') {echo 'class="current"';} ?>>中文(简体)</a></li>
                </ul>

but it doesn't work when its declared in a included file:

index.php (with included file which has the php variable declaration):

<?php
include_once 'common.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>New Project</title>
    <link rel="stylesheet" type="text/css" href="styles/global.css" />
</head>
<body id="home">
<div id="header">
    <div class="container">
        <div id="header-top">
            <h1><a href="http://widerdesign.co.nr/"&gt;wider design</a></h1>
            <ul id="lang">
                <li><a href="index.php?lang=en" <?php if($lang=='en') {echo 'class="current"';} ?>>English</a></li>
                <li><a href="index.php?lang=es" <?php if($lang=='es') {echo 'class="current"';} ?>>Español</a></li>
                <li><a href="index.php?lang=tw" <?php if($lang=='tw') {echo 'class="current"';} ?>>中文(繁體)</a></li>
                <li><a href="index.php?lang=cn" <?php if($lang=='cn') {echo 'class="current"';} ?>>中文(简体)</a></li>
            </ul>

common.php:

session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang'])) {
    $lang = $_GET['lang'];    
    // register the session and set the cookie
    $_SESSION['lang'] = $lang;    
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
} else if(isSet($_SESSION['lang'])) {
    $lang = $_SESSION['lang'];
} else if(isSet($_COOKIE['lang'])) {
    $lang = $_COOKIE['lang'];
} else {
    $lang = 'en';
}

switch ($lang) {
  case 'en':
      $lang_file = 'lang.en.php';
      break;    
  case 'es':
      $lang_file = 'lang.es.php';
      break;    
  case 'tw':
      $lang_file = 'lang.tw.php';
      break;    
  case 'cn':
      $lang_file = 'lang.cn.php';
      break;    
  default:
      $lang_file = 'lang.en.php';    
}    
include_once 'languages/'.$lang_file;

Is $lang really having any value when its declared in the common.php file?

edit: the language change in the file is working is just the class .current not being activated, weird.

+1  A: 

Switch out include_once to require_once. If you are getting errors, it means your paths are incorrect and such the files aren't loaded.

Is $lang really having any value when its declared in the common.php file?

Logically yes, but var_dump($lang); at the end of common.php will tell you for sure.

Edit: as poke suggests, the $lang might be set to anything. You should whitelist it by switching out:

if(isSet($_GET['lang'])) {
    $lang = $_GET['lang'];    
    // register the session and set the cookie
    $_SESSION['lang'] = $lang;    
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
} else if(isSet($_SESSION['lang'])) {
    $lang = $_SESSION['lang'];
} else if(isSet($_COOKIE['lang'])) {
    $lang = $_COOKIE['lang'];
} else {
    $lang = 'en';
}

to

// set default value
$lang = 'en';

// which languages are accepted?
$available_languages = array('en', 'es', 'tw', 'cn');

if(isset($_GET['lang']) && in_array($_GET['lang'], $available_languages)) {
    $lang = $_GET['lang'];    
} else if(isSet($_SESSION['lang']) && in_array($_SESSION['lang'], $available_languages)) {
    $lang = $_SESSION['lang'];
} else if(isSet($_COOKIE['lang']) && in_array($_COOKIE['lang'], $available_languages)) {
    $lang = $_COOKIE['lang'];
}

// save new value in each case
$_SESSION['lang'] = $lang;    
setcookie("lang", $lang, time() + (3600 * 24 * 30));
chelmertz
i did the var dump and the following appeared: array(1) { ["h1"]=> string(10) "Hola Mundo" }
janoChen
I still think you should refactor to the code which is mentioned in my edit.
chelmertz
+2  A: 

Yes, it will definitely be set, given that you have a if/else structure where you set the variable in each block. So after the if/else, it definitely has a value. However this value can be empty (an empty string ''), given that you just copy the value from $_GET, or $_SESSION.

To be safe, you should set $lang to en in the default part of your switch, to be really safe. Then it will always have a real value (that will also work with your application).

poke
+1  A: 

The $lang variable is set at the top of your common.php file ; and you are then trying to use it from the content of index.php.

But, between the top of common.php and the content of index.php, you are including the languages/lang.XX.php file.


Are you sure there is not something, in that lang.XX.php file, that overrides the content of the $lang variable ?

Judging from this other question (To which I answered, and this question here seems like a follow-up of that question there -- I didn't really notice that the $lang variable was being overriden), lang.en.php contains this :

<?php
$lang = array(
    'h1' => 'Hello World',
);
?>

Which overrides the $lang variable :

  • in common.php it is used to store the language code
  • in lang.en.php is it overriden, set to an array of string
  • which means that, later, in index.php, it cannot be equal to the language code -- and the condition to set the CSS class fails.
Pascal MARTIN
Yes you are right, it was being overriden.When I did `var_dump($lang);` the following appeared: `array(1) { ["h1"]=> string(10) "Hola Mundo" }` I used `$lang_file` instead: <li><a href="index.php?lang=en" <?php if($lang_file=='lang.en.php') {echo 'class="current"';} ?>>English</a></li>and it worked.
janoChen