Amy1980 Posted December 3, 2006 Share Posted December 3, 2006 [move]Hello[/move]Next question:)[code]<?php if (isset($newlang) AND !eregi("\.","$newlang")) { if (file_exists("languages/".$newlang.".php")) { setcookie("lang",$newlang,time()+31536000); include("languages/".$newlang.".php"); $currentlang = $newlang; } else { setcookie("lang",$language,time()+31536000); include("languages/".$language.".php"); $currentlang = $language; } } elseif (isset($lang)) { include("languages/".$lang.".php"); $currentlang = $lang; } else { include("languages/en.php"); $currentlang = "en"; } ?>[/code]I got something like this. On localhost the last else (default language) works great, but on server I can't change language. What is wrong with this code? Have You got any ideas?Greetings,Amy Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/ Share on other sites More sharing options...
keeB Posted December 3, 2006 Share Posted December 3, 2006 Looks like your code is sane, and if it works on localhost, my only guess is that the include path is configured differently on localhost than it is on your server.Could you please provide the error the server is returning?- Keeb Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134484 Share on other sites More sharing options...
Amy1980 Posted December 3, 2006 Author Share Posted December 3, 2006 The problem is:on localhost: when I choose any language the cookie is senton server: when I try to change language cookie isin't sent and language doesn't change.Paths are the same on localhost and webserver, I mean directory structure. Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134500 Share on other sites More sharing options...
keeB Posted December 3, 2006 Share Posted December 3, 2006 Is session support enabled on your server?Can I see the results of your[code]<?phpphpinfo();[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134523 Share on other sites More sharing options...
Amy1980 Posted December 3, 2006 Author Share Posted December 3, 2006 Session support: enabledHmm...have You got any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134537 Share on other sites More sharing options...
keeB Posted December 3, 2006 Share Posted December 3, 2006 on the next page, after the cookie is supposed to be set, please:[code]<?phpprint_r($_COOKIE);?>[/code]Is ANY cookie information being set? Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134544 Share on other sites More sharing options...
Amy1980 Posted December 3, 2006 Author Share Posted December 3, 2006 Any cookie infos isin't sent. It returns Array ( ) Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134549 Share on other sites More sharing options...
Amy1980 Posted December 3, 2006 Author Share Posted December 3, 2006 on localhost print [code]$_COOKIE [/code]returns [code]Array ( [lang] => de ) [/code] if selected German[code]Array ( [lang] => pl ) [/code] if selected Polsih[code]Array ( [lang] => en ) [/code] if selected English Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134553 Share on other sites More sharing options...
keeB Posted December 3, 2006 Share Posted December 3, 2006 Ok,Next thing we have to do is verify if it's a server problem or if it's a problem with the code being ported (maybe different settings, maybe different version, something) from server to server.Create a sample page... Page1.php[code]<?phpsetcookie("lang","en",time()+31536000);?><a href="page2.php">check cookie information</a>[/code]page2.php[code]<?phpprint "<pre>";print_r($_COOKIE);?>[/code]Let me know the result. Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134554 Share on other sites More sharing options...
Amy1980 Posted December 3, 2006 Author Share Posted December 3, 2006 On my webserver returns [code]Array( [lang] => en)[/code]I think it could be problem with the variable or php version? On localhost I use the newest, on webserver I got [code]PHP Version 4.4.4[/code]. Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134555 Share on other sites More sharing options...
redbullmarky Posted December 3, 2006 Share Posted December 3, 2006 where are $newlang, $lang, etc being first set?if these are (intended) to be set via the URL or a posted form, then change them to $_GET['newlang'] (if the URL) or $_POST['newlang'] (if from a posted form). PHP has a setting, register_globals which, if turned on, would allow you to get away without doing this. however, it's highly advisable to turn this off (the default setting now with PHP) as it opens your site up to all sorts of attack.[move]hope that helps[/move] :) Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134556 Share on other sites More sharing options...
Amy1980 Posted December 3, 2006 Author Share Posted December 3, 2006 When I use[code]setcookie("lang",en,time()+31536000);[/code]it returns[code]Array ( [lang] => en ) [/code]Cookie has been sent. But what is the main problem? Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134557 Share on other sites More sharing options...
Amy1980 Posted December 3, 2006 Author Share Posted December 3, 2006 When I change 'newlang' to '$_GET['lang'] I got:[code]Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/pedron/public_html/layout/setlang.php on line 2[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134558 Share on other sites More sharing options...
redbullmarky Posted December 4, 2006 Share Posted December 4, 2006 [code]<?php$newlang = $_GET['newlang'];$lang = $_GET['lang']; if (isset($newlang) AND !eregi("\.","$newlang")) { if (file_exists("languages/".$newlang.".php")) { setcookie("lang",$newlang,time()+31536000); include("languages/".$newlang.".php"); $currentlang = $newlang; } else { setcookie("lang",$language,time()+31536000); include("languages/".$language.".php"); $currentlang = $language; } } elseif (isset($lang)) { include("languages/".$lang.".php"); $currentlang = $lang; } else { include("languages/en.php"); $currentlang = "en"; } ?>[/code]try that. am i right then in thinking that you DO intend to get lang/newlang from the URL?[b]edit:[/b] if not (and it looks like youre trying to get $lang from a cookie) change my line to:[code]$lang = $_COOKIE['lang'];[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134561 Share on other sites More sharing options...
Amy1980 Posted December 4, 2006 Author Share Posted December 4, 2006 Ok. Now is ok.Cookie has been sent:[code]Array ( [lang] => de ) [/code]but language isin't changed.[move][b]EDIT[/b][/move]Maybe this is the problem?[code]include("languages/en.php"); $currentlang = "en";[/code]Cause it has to "read" only English language. But what to set instead?[move][b]EDIT[/b][/move]When I change [code]include("languages/en.php"); $currentlang = "en";[/code]with [code]include("languages/".$newlang.".php"); $currentlang = $newlang;[/code]or [code] include("languages/".$lang.".php"); $currentlang = $lang;[/code]or [code] include("languages/".$language.".php"); $currentlang = $language;[/code]I got these errors[code]Warning: main(languages/.php) [function.main]: failed to open stream: No such file or directory in /home/pedron/public_html/layout/setlang.php on line 19Warning: main(languages/.php) [function.main]: failed to open stream: No such file or directory in /home/pedron/public_html/layout/setlang.php on line 19Warning: main() [function.include]: Failed opening 'languages/.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/pedron/public_html/layout/setlang.php on line 19[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134562 Share on other sites More sharing options...
redbullmarky Posted December 4, 2006 Share Posted December 4, 2006 not sure i'm following you now.basically what you need to aim for is proper retrieval of get/post/cookie values. register_globals is funny like that - if it's off, you need to specify where the data is coming from.so if you're referring to something in the URL, use $_GET.if you're referring to something in a <form> with method set to 'post', use $_POST.if you're referring to a cookie, use $_COOKIE.can you explain your code a little better and try and isolate where the issue is? Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134564 Share on other sites More sharing options...
Amy1980 Posted December 4, 2006 Author Share Posted December 4, 2006 Thanx! It works now!I changed GET['lang'] to COOKIE['lang']Thanks once again!BTW PHP-Freaks is the most helpful phorum over the Net IMHO Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134565 Share on other sites More sharing options...
redbullmarky Posted December 4, 2006 Share Posted December 4, 2006 lol no probs.for the future, i'd strongly strongly strongly (STRONGLY) recommend always turning register_globals off in your php.ini file (most default installations/webhosts do this by default these days, hence why your site works differently on localhost where you probably have it turned on) and using $_GET/$_POST/$_COOKIE instead of just the variable name . sure, it means it takes a little longer to type ($_GET/$_POST/$_COOKIE/$_FILES) but it has two advantages. 1) you close many security loopholes and 2) it generally makes your code easier to follow - ie, you can clearly see that $_GET['lang'] / $_COOKIE['lang'] means a URL variable ?lang=en or a cookie, whereas if it's just $lang, you wont have a clue.cheersMark Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134568 Share on other sites More sharing options...
Amy1980 Posted December 4, 2006 Author Share Posted December 4, 2006 HmmmBut when there's no cookie [user hasen't selected lang yet] I got:[code]Warning: main(languages/.php) [function.main]: failed to open stream: No such file or directory in /home/pedron/public_html/layout/setlang.php on line 18Warning: main(languages/.php) [function.main]: failed to open stream: No such file or directory in /home/pedron/public_html/layout/setlang.php on line 18Warning: main() [function.include]: Failed opening 'languages/.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/pedron/public_html/layout/setlang.php on line 18[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134569 Share on other sites More sharing options...
redbullmarky Posted December 4, 2006 Share Posted December 4, 2006 you're referring to a variable called $language. where is this coming from? Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134571 Share on other sites More sharing options...
Amy1980 Posted December 4, 2006 Author Share Posted December 4, 2006 Hmm. the best way is to show You all the code. Here You are:[b]index.php [main page][/b][code]<?phprequire_once ("setlang.php");?>[/code][b]setlang.php [changes languages][/b][code]<?php$newlang = $_GET['newlang'];$lang = $_COOKIE['lang']; if (isset($newlang) AND !eregi("\.","$newlang")) { if (file_exists("languages/".$newlang.".php")) { setcookie("lang",$newlang,time()+31536000); include("languages/".$newlang.".php"); $currentlang = $newlang; } else { setcookie("lang",$language,time()+31536000); include("languages/".$language.".php"); $currentlang = $language; } } elseif (isset($lang)) { include("languages/".$lang.".php"); $currentlang = $lang; } else { include("languages/".$language.".php"); $currentlang = $language; } ?>[/code][b]setlang2.php [redirecting to index.php to drop ?newlang=de fx. from URL][/b][code]<?phprequire_once ("setlang.php");header("Location: index.php");?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134572 Share on other sites More sharing options...
redbullmarky Posted December 4, 2006 Share Posted December 4, 2006 if this is your whole file:[code]<?php$newlang = $_GET['newlang'];$lang = $_COOKIE['lang']; if (isset($newlang) AND !eregi("\.","$newlang")) { if (file_exists("languages/".$newlang.".php")) { setcookie("lang",$newlang,time()+31536000); include("languages/".$newlang.".php"); $currentlang = $newlang; } else { setcookie("lang",$language,time()+31536000); include("languages/".$language.".php"); $currentlang = $language; } } elseif (isset($lang)) { include("languages/".$lang.".php"); $currentlang = $lang; } else { include("languages/".$language.".php"); $currentlang = $language; } ?>[/code]then line 18 (where your error is) is:[code] include("languages/".$language.".php");[/code]right?i'm assuming that this, as well as:[code] } else { setcookie("lang",$language,time()+31536000); include("languages/".$language.".php"); $currentlang = $language; }[/code]is some kinda fallback incase no cookies have been sent and no language as been specified in the URL. if that's the case, default to english (or whatever language you wanna default to):[code]<?php$newlang = $_GET['newlang'];$lang = $_COOKIE['lang'];$language = 'en'; // default to 'en' if no lang specified if (isset($newlang) AND !eregi("\.","$newlang")) { if (file_exists("languages/".$newlang.".php")) { setcookie("lang",$newlang,time()+31536000); include("languages/".$newlang.".php"); $currentlang = $newlang; } else { setcookie("lang",$language,time()+31536000); include("languages/".$language.".php"); $currentlang = $language; } } elseif (isset($lang)) { include("languages/".$lang.".php"); $currentlang = $lang; } else { include("languages/".$language.".php"); $currentlang = $language; } ?>[/code]give that a blast Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134573 Share on other sites More sharing options...
Amy1980 Posted December 4, 2006 Author Share Posted December 4, 2006 LOL.I have done it myself ;DBut with variable default. But thankx for the helping me! I'm appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/29332-isset-problem/#findComment-134575 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.