Jump to content

Language Session not being sent to pages


barney0o0

Recommended Posts

Hi Folks

 

At the top of each page i have

 

<?php $_SESSION['lang'] = (isset($_GET['lang'])) ? $_GET['lang'] : "en"; ?>
<?php $suffix = mysql_escape_string($_SESSION['lang']);?>

 

Throughout the site a have 2 buttons (one english and one italian) which has

<a href="?lang=it"

 

..now this refreshes the same page fine, however if i change the language, then go to another page it defaults back to english...

 

Sorry, ive been working on this site off and on, mades changes and taken bit from here an there so i just lost the flow.

 

Many thanks in advance

Link to comment
Share on other sites

try using this...

 

<?php
session_start();
$_SESSION['lang'] = (isset($_GET['lang'])) ? $_GET['lang'] : "en";
?>
<?php $suffix = mysql_escape_string($_SESSION['lang']);?>

 

tell me the results plz.

Link to comment
Share on other sites

Cheers, however it still doesnt work...no errors, just go back the default english...

 

i used ;

 

<?php
session_start();
$_SESSION['lang'] = (isset($_GET['lang'])) ? $_GET['lang'] : "en";
?>
<?php $suffix = mysql_escape_string($_SESSION['lang']);?>

Link to comment
Share on other sites

erm...well, on the 2 main buttons (for selecting the language) i just used;

 

<a href="?lang=it"

 

 

...wouldnt  this be adequate in setting the session variable?

 

Conker87

 

so i need to put the session literally at the top?

 

ive got;

 

<?php require_once('Connections/***.php'); ?>
<?php
session_start();
$_SESSION['lang'] = (isset($_GET['lang'])) ? $_GET['lang'] : "en";
?>
<?php $suffix = mysql_escape_string($_SESSION['lang']);?>

 

...would my original code be ok, but just moving the session position? (sorry i dont want to start cutting and pasting site wide nitl i know im going in the right direction!)

Link to comment
Share on other sites

hum..i used another way (that i did originally, then knocked it about a bit), and it works (however i also empty the broser cache (if it makes any difference)

 

<?php
session_start();
if (isset($_GET['lang'])) {
$lang=$_GET['lang'];
$_SESSION['lang'] = $lang;
}
if(!isset($_SESSION['lang'])){
$lang="it";
}else{
$lang = $_SESSION['lang'];
}
?>

 

 

Link to comment
Share on other sites

oh dear, i spoke way too soon...

 

Ive just tried the pages, and it doesn't default to any language (en), i just an database error. All my databases have a suffix, for example menu_en, menu_it that it derived from session or the language button.

 

So ive just tried a page and it says that database 'menu_' doesn't exist (obviously as it hasnt picked up the session).

 

Could someone make a suggestion...

 

Many thanks in advance

Link to comment
Share on other sites

Thanks conker, works like a dream, however now ive come across another problem with the language buttons themselves!

 

 

If, for example im on page

 

http://www.artsouk.biz/about_detail.php?ID=2

 

..then, use the lang button it goes to

 

 

http://www.artsouk.biz/about_detail.php?lang=en

 

...and i loose the id of the page and its contents......(the pages are online, so you can give it a try and see for yourself)

Link to comment
Share on other sites

Try:

 

<?php

if ($_SERVER['QUERY_STRING'])
       {
              echo "<a href=\"{$_SERVER['QUERY_STRING']}&lang=it\">Italian</a>";
              echo "<a href=\"{$_SERVER['QUERY_STRING']}&lang=en\">English</a>";
       }
else
       {
              echo "<a href=\"?lang=it\">Italian</a>";
              echo "<a href=\"?lang=en\">English</a>";
       }

?>

 

I haven't tested this, nor do I remember if that's how you can use QUERY_STRING, but give it a shot anyway.

Link to comment
Share on other sites

Hey conker..

 

Ive actually got it working with the additional '?'...all seems fine....OR, do you think it'd be better to extend the script like youve just suggested?

 

what im using;

 

<div id="lang">

<?php

if ($_SERVER['QUERY_STRING'])
       {
             
              echo "<a href=\"?{$_SERVER['QUERY_STRING']}&lang=en\">ENGLISH</a>";
       }
else
       {
             
              echo "<a href=\"?lang=en\">ENGLISH</a>";
       }

?>
| <?php

if ($_SERVER['QUERY_STRING'])
       {
             
              echo "<a href=\"?{$_SERVER['QUERY_STRING']}&lang=it\">ITALIANO</a>";
       }
else
       {
             
              echo "<a href=\"?lang=it\">ITALIANO</a>";
       }

?> </div>

Link to comment
Share on other sites

If you include all of your pages in your index page, then your code will work great. However, if you have different page (like home.php?lang=it, contact.php?lang=it etc) then you'll need to give the php_self too. :)

Link to comment
Share on other sites

hum..ive just realised, that if im on one page and keep changing the language i get

 

http://www.artsouk.biz/computerised_detail.php?ID=2&lang=it&lang=en&lang=it&lang=en&lang=it

 

...also, im i open to any security issues when i use this method?

Link to comment
Share on other sites

Just real escape it.

 

Use a search string, search for the lang, if it's there, trim the string down 7 characters. I've had the same problem when adding styles etc to urls. I just gave up and used database choices.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.