Answer the question
In order to leave comments, you need to log in
Self-written PHP function not working on hosting?
There is a function that translates some blocks of text:
<?php
function translate($rus,$eng)
{
$lang = $_GET['lang'];
$cookie_fresh = mktime(23,59,59,12,31,date('Y')+1);
if ($lang == 'eng')
{
setcookie('lang', 'eng', $cookie_fresh);
header("Location: ./");
}
if (($lang == 'rus') or (!isset($_COOKIE['lang'])))
{
setcookie('lang', 'rus', $cookie_fresh);
header("Location: ./");
}
if ($_COOKIE['lang'] == 'eng')
{
echo $eng;
}
if ($_COOKIE['lang'] == 'rus')
{
echo $rus;
}
return true;
}
?>
<?php translate('Русские буквы', 'English words'); ?>
Answer the question
In order to leave comments, you need to log in
My option for warming up the mind:
function translate($rus, $eng)
{
static $lang;
if (isset($_GET['lang']))
{
$lang = $_GET['lang'] == 'eng' ? 'eng' : 'rus';
setcookie('lang', $lang, time() + 3600 * 24 * 365, '/');
unset($_GET['lang']);
}
elseif (empty($lang))
$lang = $_COOKIE['lang'] == 'eng' ? 'eng' : 'rus';
echo $$lang;
}
1) At the very beginning of the script, write a function call:
ob_start();
so that all output is cached and returned to the browser at the end of the script.
This will help to get rid of the “Cannot modify header information...” warnings so that they do not interfere yet ...
If there are such warnings anyway, then something is still displayed before the ob_start () call. For example, something may be before the first "<?php" (these may be invisible utf8 characters)
2) The function itself cannot “do not work” if it is declared and called. Therefore, just “debug the code” - add to all key places (before the call, in the function itself at the beginning of the call, in the if body that should be executed, etc.) echo "XX \ n";
XX - numbers in order or words. And analyze what will be output.
If there are no other debugging methods, then this method helps to quickly localize the place where the code is not executed as we would like ...
Also, the var_dump($_COOKIE); output can help. to be sure that the cookie came from the browser.
And, just in case, in the line:
$lang = $_GET['lang'];
if the "lang" attribute is not passed in the URL, then there will be an error in this place - there is no such index in the array.
We should, for good, something like this:
$lang = isset($_GET['lang'])? $_GET['lang']: '';
Probably the problem is that the file with the function is not included.
Check if the time on the server is correct? Is it correct on the computer?
As far as I know, the setcookie function does not change the $_COOKIE array further in the script, only after a reboot, perhaps this is the case.
header("Location: ./"); - This scares me a lot. Better use an absolute path.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question