barney0o0 Posted May 6, 2008 Share Posted May 6, 2008 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 Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted May 6, 2008 Share Posted May 6, 2008 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. Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 6, 2008 Author Share Posted May 6, 2008 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']);?> Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted May 6, 2008 Share Posted May 6, 2008 When the user selects a session, did you remember to register that session? Quote Link to comment Share on other sites More sharing options...
conker87 Posted May 6, 2008 Share Posted May 6, 2008 You don't need to register sessions... Make sure session_start(); is at the VERY top of the page. Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 6, 2008 Author Share Posted May 6, 2008 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!) Quote Link to comment Share on other sites More sharing options...
conker87 Posted May 6, 2008 Share Posted May 6, 2008 session_start(); Must be before absolutely anything is outputted. For instance, if you've got: <html> </html> As a page, then you must place it here: <?php session_start(); ?> <html> </html> Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 6, 2008 Author Share Posted May 6, 2008 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']; } ?> Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 7, 2008 Author Share Posted May 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
conker87 Posted May 7, 2008 Share Posted May 7, 2008 You could amend the SQL in by adding: SELECT * FROM `menu_$lang` etc Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 7, 2008 Author Share Posted May 7, 2008 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) Quote Link to comment Share on other sites More sharing options...
saint959 Posted May 7, 2008 Share Posted May 7, 2008 hi barney, try this: SELECT * FROM `menu_".$lang."` etc Quote Link to comment Share on other sites More sharing options...
conker87 Posted May 7, 2008 Share Posted May 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 7, 2008 Author Share Posted May 7, 2008 Cheers conker..nearly there! When im on http://www.artsouk.biz/about_detail.php?ID=1 and hover over the language buttons, it doesnt include the actual page, however it does detect the ID and language ie. on hover i get artsouk.biz/ID=1&lang=it Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 7, 2008 Author Share Posted May 7, 2008 oh, i just cahged the script to "<a href=\"?{$_SERVER['QUERY_STRING']}&lang=it\">Italiano</a>"; and i thinks its working...back in a mo Quote Link to comment Share on other sites More sharing options...
conker87 Posted May 7, 2008 Share Posted May 7, 2008 OK, try: "<a href=\"{$_SERVER['PHP_SELF']}?{$_SERVER['QUERY_STRING']}&lang=it\">Italiano</a>"; Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 7, 2008 Author Share Posted May 7, 2008 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> Quote Link to comment Share on other sites More sharing options...
conker87 Posted May 7, 2008 Share Posted May 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 7, 2008 Author Share Posted May 7, 2008 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? Quote Link to comment Share on other sites More sharing options...
conker87 Posted May 7, 2008 Share Posted May 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 7, 2008 Author Share Posted May 7, 2008 i feel i bit dumb....i know that you helped me loads, however could you impliment the escapestring/trim to the script that im using... ..then im sure ill leave you alone! Many thanks with your help on this Quote Link to comment Share on other sites More sharing options...
conker87 Posted May 7, 2008 Share Posted May 7, 2008 The real_escape bit is easy. In your SQL, change the $lang value to mysql_real_escape_string($lang); And I can't help you with the trim, sorry, string finding functions scare me. *twitch* Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 7, 2008 Author Share Posted May 7, 2008 so would this surfice? else{ $lang = mysql_escape_string($_SESSION['lang']); } ?> Quote Link to comment Share on other sites More sharing options...
conker87 Posted May 7, 2008 Share Posted May 7, 2008 Aye Quote Link to comment Share on other sites More sharing options...
barney0o0 Posted May 7, 2008 Author Share Posted May 7, 2008 many thanks b Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.