views:

41

answers:

5

In this file (localization.php):

<?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';
}

// use appropiate lang.xx.php file according to the value of the $lang
switch ($_SESSION['lang']) { //line 23
case 'en':
case 'es':
case 'zh-tw':
case 'zh-cn':
    $lang_file = 'lang.'.$_SESSION['lang'].'.php';
    break;

default:
    $lang_file = 'lang.en.php';
}
//localization helper function
function l($localization) {
    global $lang;
    return $lang[$localization];
}
    include_once 'languages/'.$lang_file;
?>

I get the following warning message (localization.php):

Notice: Undefined index: lang in 

D:\wamp\www\projects\alexchen\alexchen2.0\localization.php on line 23

But in this another similar file I don't get that warning:

<?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';
}

// use appropiate lang.xx.php file according to the value of the $lang
switch ($lang) {
case 'en':
    $lang_file = 'lang.en.php';
    break;

case 'es':
    $lang_file = 'lang.es.php';
    break;

case 'zh-tw':
    $lang_file = 'lang.zh-tw.php';
    break;

case 'zh-cn':
    $lang_file = 'lang.zh-cn.php';
    break;

default:
    $lang_file = 'lang.en.php';
}

//translation helper function
function l($localization) {
    global $lang;
    return $lang[$localization];
}

    include_once 'languages/'.$lang_file;
?>

index.php:

    <?php
    include_once 'localization.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 name="robots" content="index, follow"/>
        <meta name="description" content="Web design and Translation / 網頁設計和翻譯" />
        <meta name="keywords" content="web development, web developer, web design, web designer, translation, translator, taiwan, taipei, taichung, english, chinese, spanish, 網站開發者, 網頁設計, 網頁設計師, 翻譯, 翻譯著, 台灣, 台北, 台中, 英文, 中文, 西班牙文, html, css, javascript, php" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
        <title>Alex Chen - Web design and Translation / 網頁設計和翻譯</title>
        <link rel="stylesheet" type="text/css" href="styles/reset.css" />
        <link rel="stylesheet" type="text/css" href="styles/global.css" />
        <link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.1.css" />
        <?php if($lang_file=='lang.zh-tw.php' || $lang_file=='lang.zh-cn.php') {echo '<link rel="stylesheet" type="text/css" href="styles/chinese.css" />';} ?>
    </head>
    <body id="home">
    <div id="header">
        <div class=

"container">

(etc.)

I know they do basically the same but why an I getting that warning from the first localization.php file? How can I fix it?

+1  A: 

It's not similar. In one you have

switch ($_SESSION['lang']) { //line 23

In the other

switch ($lang) {

In the first case, you get a notice because... There's no element named lang in the array $_SESSION. In the second you get no warning because $lang is always defined.

Artefacto
+1  A: 

Look closely at the control flow of the first one? $lang is set even if $_SESSION['lang'] is undefined. Then you throw out all these undefined-variable fallbacks out of the window by using $_SESSION['lang'] directly anyway in the switch() statement.

A: 

Because you don't check if the $_SESSION['lang'] variable is set, within the switch statement.

Chris
A: 

the reason is obvious: in the first one you don't have $_SESSION['lang'] variable set.
use $lang instead.

Col. Shrapnel
A: 
 switch ($_SESSION['lang'])// lang is not yet a key of $_SESSION

Try something like this

 if(!array_key_exists('lang',$_SESSION))
 {
        $_SESSION['lang']=some_value;
 }
 else if(..)
 {

        //remaining code
 }
Prasoon Saurav