-1

i am try to make a multilingual website following this example. i made a folder language and created 2 files the en.php and the gr.php.

here are my files

language/gr.php

<?php
$lang = array(
//nav bar
'nav1' => 'Αρχική',
'nav2' => 'Δυτική Αττική',
'nav3' => 'Εκδηλώσεις',
'nav4' => 'Σύλλογοι',
'nav5' => 'Καταστήματα'
);
?>

language/en.php

<?php
$lang = array(
//nav bar
'nav1' => 'Home',
'nav2' => 'West Attica',
'nav3' => 'News/Events',
'nav4' => 'Associations',
'nav5' => 'Stores'
 );
?>

and the ini.php

<?php
session_start();

$allowed_lang = array('gr', 'en');

if (isset($_GET['lang']) === true && in_array($_GET['lang'], $allowed_lang) === true){
$_SESSION['lang'] = $_GET['lang'];
}

include 'language/' . $_SESSION['lang'] . '.php';

 ?>

when i am try to run it i get this errors

Notice: Undefined index: lang in /home/user/public_html/init.php on line 10
Warning: include(language/.php): failed to open stream: No such file or directory in /home/peerkavlos/public_html/init.php on line 10
Warning: include(): Failed opening 'language/.php' for inclusion (include_path='.:/usr/share/php') in /home/user/public_html/init.php on line 10

tried to echo the $_GET['lang']; but the results has no value

that means that the $_GET['lang'] is empty

what am i missing guys?

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
vaggelis
  • 60
  • 2
  • 9

2 Answers2

0

You don't have a default, so when the _GET variable is empty, you get an include on a file that doesn't exist. Maybe just hard code a default:

if (empty($_SESSION['lang'])) {
    $_SESSION['lang'] = 'en';
}
if (isset($_GET['lang']) ... ) {
}
include 'language/' . $_SESSION['lang'] . '.php';
Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
0

try this

if (!empty($_GET['lang']) && in_array($_GET['lang'], $allowed_lang)) 
{
    # TRUE part
    $_SESSION['lang'] = $_GET['lang'];
    include 'language/' . $_SESSION['lang'] . '.php';
} 
else 
{
    # FALSE part
    echo "Selected Language Couldn't initialize"; 
    include 'language/en.php';
}

Useful links

  1. PHP in_array() Function
  2. PHP 5 Sessions
  3. isset() and empty() - what to use
Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85