A
A
Artqookie2011-03-27 14:00:35
PHP
Artqookie, 2011-03-27 14:00:35

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;	
}
?>

The transformation goes like this:
<?php translate('Русские буквы', 'English words'); ?>

On LAMP'e, on LAN, everything is top type. But when I upload to shared hosting, no text is displayed. What could be the problem?

Answer the question

In order to leave comments, you need to log in

10 answer(s)
A
Alexey, 2011-03-27
@Artqookie

error_reporting(E_ALL);
ini_set('display_errors', true);

S
Shvonder, 2011-03-27
@Shvonder

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;	
}

H
Horzerus, 2011-03-27
@Horzerus

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']: '';

A
Alexander, 2011-03-27
@disc

Probably the problem is that the file with the function is not included.

S
Shvonder, 2011-03-27
@Shvonder

Check if the time on the server is correct? Is it correct on the computer?

B
bruteo, 2011-03-27
@bruteo

$lang = $_GET['lang'];
replace with $_COOKIE['lang']

A
Anatoly, 2011-03-27
@taliban

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.

D
DileSoft, 2011-03-31
@DileSoft

header("Location: ./"); - This scares me a lot. Better use an absolute path.

M
MNX, 2011-04-01
@MNX

I'll do my bit - maybe it's the UTF-8 BOM.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question