Jump to content

Isset problem


Amy1980

Recommended Posts

[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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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]
<?php
setcookie("lang","en",time()+31536000);
?>
<a href="page2.php">check cookie information</a>
[/code]

page2.php
[code]
<?php
print "<pre>";
print_r($_COOKIE);
?>
[/code]

Let me know the result.
Link to comment
Share on other sites

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] :)
Link to comment
Share on other sites

[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]
Link to comment
Share on other sites

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 19

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 19

Warning: 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]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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.

cheers
Mark
Link to comment
Share on other sites

Hmmm


But 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 18

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 18

Warning: 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]
Link to comment
Share on other sites

Hmm. the best way is to show You all the code. Here You are:

[b]index.php [main page][/b]
[code]<?php
require_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]<?php
require_once ("setlang.php");
header("Location: index.php");
?>[/code]

Link to comment
Share on other sites

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
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.