problems with inheritance of variables over many pages - php

I have this code on bootconfig.php that is loaded in all of my pages.
// What languages do we support
$available_langs = array('en','it');
// Set our default language session
$_SESSION['lang'] = 'en';
if(isset($_GET['lang']) && $_GET['lang'] != ''){
// check if the language is one we support
if(in_array($_GET['lang'], $available_langs))
{
$_SESSION['lang'] = $_GET['lang']; // Set session
}
}
// Include active language
include($_SESSION['path'].'languages/'.$_SESSION['lang'].'/lang.'.$_SESSION['lang'].'.php');
//Include LIB
include($_SESSION['path']."lib/auth.class.php");
include($_SESSION['path']."lib/register.class.php");
include($_SESSION['path']."lib/string.class.php");
//Include Other Config File
require_once('./config/stringsecure.php');
require_once("./config/menuconfig.php");
require_once("./config/headerconfig.php");
require_once("./config/contentconfig.php");
require_once("./config/forumconfig.php");
require_once("./config/titleconfig.php");
require_once("./include/vocaboli/vocaboliconf.php");
in the file lang.en.php i have for example $lang['contactme'] but if i use it in menuconfig.php it doesn't exist...
if i put echo $lang['contactme'] at the end of bootconfig it works but in menuconfig not. why?

Probably it is a simple scoping issue. Probably in one of the cases you are inside a function.
Inside a function you can't access global variables.
Probably $GLOBALS['lang']['contactme'] would work.
See the details here:
http://www.php.net/manual/en/language.variables.scope.php

As it stands here menuconfig will appear in bootconfig but not the other way around.
I think the includes are backwards: if you want $lang to appear in menuconfig you need to use require_once( 'bootconfig.php' ) in that file.

Related

External language file

I have a little bit confusing problem.
I have website with multiple languages. For default labels and headings I made some external php file where I have variables with values for different languages.
For example, In file I have variable
$heading = "First Heading in English"
and variable
$heading = "First Heading in German"...
In session I have stored value for current language, and with if state I know what language variables to take.
My problem is next:
When I load my page for first time, all of fields where I call variables from external language file are empty...
And, when I refresh my page, variables are there, with right value....
Can someone help me with this problem??
I include external file before everything in my php file, with include function.
How do you include the file?
Perhaps first language variable is not saved in session yet.
It is always a good idea to check if language is not set you are going to use some default value i.e english.
if (!isset($language)) {
$language = 'en';
}
Modified this a little bit from what i have written a while ago...maybe this will help you a little bit.
$langSession = $_SESSION['lang'];
if(!isset($_SESSION['lang']){ // if the Session with language was not set
$browserlang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); // Get the browser setting language, tested it in firefox
if($browserlang == "de"){
$_SESSION['lang'] = "de"; // German
}elseif($browserlang == "nl"){ // Dutch
$_SESSION['lang'] = "nl";
}else{ // Else English
$_SESSION['lang'] = "en";
}
}
include('language.php');

Include PHP code with the same ID

My problem is include() doesn't work in this example:
...
$lang = $_GET["lang"];
$id = $_GET["id"];
if ($lang == "fr"){
include ('indexFr.php?id='.$id);
}
else if ($lang == "ar"){
include ('indexFr.php?id='.$id);
}
else if ($lang == "en"){
include ('indexFr.php?id='.$id);
}
...
I work with this:
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "fr":
header("Location: indexFr.php?id=".$id);
break;
case "ar":
header("Location: indexAr.php?id=".$id);
break;
case "en":
header("Location: indexEn.php?id=".$id);
break;
default:
header("Location: indexEn.php?id=".$id);
break;
}
But if I want to include something else (not language page) I think this is the right code but, it doesn't work:
include ('www.monsite.com/indexFr.php?id='.$id);
How can I do it?
If your $_GET array already has a value for id, you don't need that query string on the end if you're doing an include. It will use the $_GET array you already have and get the same $_GET['id'] value.
include is, in effect, putting the code of the external file into the PHP code that is already running. So, for example, if you have this file:
index.php?id=5
echo $_GET['id'];
include "otherfile.php";
And then this other file:
otherfile.php
echo $_GET['id'];
The output will be:
55
Because you are effectually creating a file that looks like this:
echo $_GET['id'];
echo $_GET['id'];
The include tag doesn't work with query strings because since it's a local file, the query string isn't used.
If you want to include the file from a different domain, you could try:
include ('http://www.monsite.com/indexFr.php?id='.$id);
That said, including files cross-domain is considered bad practice and will possibly just include the generated HTML and not the PHP. If you're including a file from your local filesystem, you really should just use the $_GET variable that's already there.
You need to specify a full URL for this to work. What you're specifying will look for a file on your local filesystem called indexFr.php?id=123, which is not what you're trying to do. You need an http:// or https:// in there, so it knows to go through a web-server, which will pass your arguments.
http://www.php.net/manual/en/function.include.php
Indeed, they provide an example case which matches yours quite closely:
// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';
// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

PHP add GET variable to links

I have a page with links that looks like this:
...
I also may have a GET variable ?lang=en in the current url. How can I add the GET variable ?lang=en to all the links from the page, without adding them manually or adding a variable to each link? Thanks.
set $_ENV['lang'] = 'en' in your basic ( assume config.php ) file
and retrieve by
getenv('lang') or $_ENV['lang']
Refernece
Add lang=en to session variable as $_SESSION['lang']='en' & initialized it in every page.
You could use output buffering or an Apache filter to parse your output to automatically add the lang query string parameter to all links, however, this isn't terribly efficient.
If you really do not want to add it manually to each link then I suggest you store it in a session variable.
At the start of each page (perhaps in a generic include script) you could have something like:
<?php
define('DEFAULT_LANG', 'en_GB');
session_start();
// check if a new lang has been specified.
if (isset($_GET['lang'])) {
// yes, so use the requested lang
$_SESSION['lang'] = $_GET['lang'];
// otherwise, check if a lang was previously set
} else if ( ! isset($_SESSION['lang']) ) {
// no, so use default lang:
$_SESSION['lang'] = DEFAULT_LANG;
}
?>
You should then use $_SESSION['lang'] instead of $_GET['lang'] in the rest of your page. Also, you may want to add some sort of validation to ensure the requested lang is valid and available.

PHP changing language version

I have a website which i want to create other language version.
I don't want to create folder for each language. I was wondering it
it's possible to add a combobox on each page or on the main one
so that user can setup the language then using php i will
check the option and show the right version. Any suggesting
to achive that?
If you have a combobox, when the user submits it, store the language in the session (session_start(); has to be called) with $_SESSION['lang'] = $_POST['lang'];. I'd advise you to whitelist languages as such:
session_start();
// define language whitelist
$allowedLangs = array('en', 'de');
// only store the new user language if it's an allowed one
if (isset($_POST['lang']) && in_array($_POST['lang'], $allowedLangs)) {
$_SESSION['lang'] = $_POST['lang'];
}
// define the user language based on session data or use 'en' as default if not available
$userLang = isset($_SESSION['lang']) ? $_SESSION['lang'] : 'en';
// parse some language file according to the language
$translations = // TODO load some file with $userLang here
Of course you should adjust this to your own project and environment. For translation files, you can use a plain PHP file that returns an array like such:
<?php
// en.php
return array(
'some.key' => 'Translation',
);
Then if you include that file, the return value of the include will be the array, so in the above code you could do:
$translations = include 'translations/'.$userLang.'.php';
You then have to output all your text through this $translations variable, like echo $translations['some.key'].
if you wanted to use cookies... in the lang files you would include an array of words or content to use.
<?php
if($_GET['language']){
$lang = (string)$_GET['language'];
setcookie("lang", $lang, time()+3600);
header('Location: '.$_SERVER['PHP_SELF']);
die();
}elseif(!isset($_COOKIE['lang'])){
$lang='en';
}else{$lang=$_COOKIE['lang'];}
switch($lang){
case "en":
include('./lang/en.php');
break;
case "fr":
include('./lang/fr.php');
break;
case "pol":
include('./lang/pol.php');
break;
default:
include('./lang/en.php');
break;
}
?>
you mean something along the lines of
if ($_GET['language']) {
include $_GET['language'] . ".php";
}
and then save the languages in a php-file with there name, or a function depending on what you want to do with it
hey for language version.
have languages in combobox.
maintain your current language in session.
When u change language call an ajax call Update the changed language into session and reload the page.
display page view with respect to session stored language.
thats it........

Help on changing the page language without changing the current page (PHP)

langauage web. I set 3 links, Français... (href=changeLanguage.php?lang=fr,es,en)
changLanguage.php
session_start();
if(isset($_SESSION['bckTo']) && isset($_GET['lang'])){
$lang = preg_replace('/[^a-z]/','',trim($_GET['lang']));
#TODO
#More vlidation ...
$full_url = $_SESSION['bckTo'];
$full_url = str_replace(array('&lang=en','&lang=es','&lang=fr'),'',$full_url);
header('Location: '.$full_url.'&lang='.$lang.'');
}
$_SESSION['bckTo'] save the current URL for example: mysite.com/index.php?id=x&n_d=y
The problem is, the header translate the URL to: mysite.com/index.php?id=x&n_d=y&lang=en.
Any idea will be appreciate
Why not just set the language in session instead of via GET? Then you just have to put a link to change the language and and then redirect to the page. This would probably be best, given that you are already using sessions.
Example:
English
On the changeLanguage:
//code up here
if (isset($_SESSION['bckTo') && isset($_GET['lang'])) {
// $lang code here
$_SESSION['lang'] = $lang;
header('Location: ' . $_SESSION['bckTo']);
}
Then you would just need to change your language checking / displaying code to check the session variable rather than the GET (on the actual pages).
Running html_entity_decode will convert those HTML entities back into ampersands.
http://us2.php.net/manual/en/function.html-entity-decode.php

Categories