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 Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/ 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. Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-534400 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']);?> Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-534403 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? Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-534427 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. Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-534429 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!) Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-534436 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> Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-534439 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']; } ?> Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-534455 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 Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535030 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 Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535070 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) Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535077 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 Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535080 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. Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535089 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 Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535095 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 Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535097 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>"; Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535129 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> Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535131 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. Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535135 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? Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535141 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. Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535143 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 Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535146 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* Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535152 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']); } ?> Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535166 Share on other sites More sharing options...
conker87 Posted May 7, 2008 Share Posted May 7, 2008 Aye Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535187 Share on other sites More sharing options...
barney0o0 Posted May 7, 2008 Author Share Posted May 7, 2008 many thanks b Link to comment https://forums.phpfreaks.com/topic/104384-language-session-not-being-sent-to-pages/#findComment-535191 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.