-1

I have an old calculator on one of my websites and after recently noticed that it no longer loads (the code is pretty ancient). It just displays a white blank page with no errors. The only error I could find was in the server error log that says:

PHP Warning: Illegal string offset 'action' in /home/o8ap61wv26c5/public_html/calculator.php on line 13

this is the code it is referring to:

    $param = get_request_var('param');

if($param['action'] == 'calculate'){
    $param['principal'] = preg_replace('/[^0-9\.]/', '', $param['principal']);
    if($param['principal'] < $config['min_principal'] || $param['principal'] > $config['max_principal']) $err['principal'] = true;
    if($param['interest_rate'] < $config['min_interest_rate'] || $param['interest_rate'] > $config['max_interest_rate']) $err['interest_rate'] = true;
    if($param['start_year'] < $config['min_start_year'] || $param['start_year'] > $config['max_start_year']) $err['start_year'] = true;
    if($param['start_month'] < 1 || $param['start_month'] > 12) $err['start_month'] = true;
    if($param['term'] < $config['min_term'] || $param['term'] > $config['max_term']) $err['term'] = true;
}

and this is the function code

function get_request_var($varname = '', $defaultval = '') {
global $smarty;
if($varname){
    if(isset($_POST[$varname])){
        $var = $_POST[$varname];
    } elseif(isset($_GET[$varname])) {
        $var = $_GET[$varname];
    }
} elseif(count($_POST)) {
    $var = $_POST;
} elseif(count($_GET)) {
    $var = $_GET;
}
$smarty->load_filter('output', 'correctoutput');
if(isset($var)){
    /* assign variable to Smarty */
    if(isset($smarty)){
        if(get_magic_quotes_gpc()){
            $smarty->assign($varname, array_stripslashes($var));
        } else {
            $smarty->assign($varname, $var);
        }
    }

    /* adding slashes if magic quotes feature is turned off */
    if(!get_magic_quotes_gpc()) $var = array_addslashes($var);

    return $var;
} else {
    $smarty->assign($varname, $defaultval);
    return $defaultval;
}

}

any idea why the page would give this error? Also worth mentioning is that I recently moved serves php 5.6

Sean
  • 3
  • 3
  • 1
    `print_r($param)` – pavel Dec 27 '21 at 09:28
  • You're using `$param['action']` without checking if it exists or not, and when it does not, your code that uses it will throw that warning. That sounds like a very old version of PHP - I can't quite get what your last sentence is trying to say. Did you move __to__ a server that uses PHP 5.6, or did you move __from__ that version? If it was the latter, what version are you on now? – droopsnoot Dec 27 '21 at 09:41
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Nico Haase Dec 27 '21 at 10:06
  • Thanks for the help, what I meant was that the current server is running php 5.6 and I'm not sure what my previous server was running. However, upon researching for hours I found out the problem was not mainly code. It was actually the server configuration. By default the server had zlib.output_compression = off and I needed to set this to on zlib.output_compression = On and everything started working as it should. – Sean Dec 27 '21 at 22:03

2 Answers2

0

You have to check if action exists in your params

if ( isset ( $param['action'])) {}

nice way to print your indexes :

echo "<pre>".print_r($param, true)."</pre>";

For the why it's not working anymore, maybe your server change it's version of it error reporting is not higher than before ?

mt.i.1
  • 115
  • 7
  • Thank you for your help on this. The issue was the servers PHP configuration. I stumbled upon it by pure luck on another post and it seems that all I had to do was set this setting to on, zlib.output_compression = On – Sean Dec 27 '21 at 22:04
  • You should still correct your condition by adding the *isset* part, just to be sure this error won't come out again ;) – mt.i.1 Dec 28 '21 at 08:03
  • I took your advice and did that for this part of the code and did a search for the remaining files that did the same thing and replaced them. Thank you for the recommendation, I appreciate your help! – Sean Dec 28 '21 at 23:48
  • Please, don't forget to accept/upvote the answer :) – mt.i.1 Dec 30 '21 at 11:16
  • Sorry I'm somewhat new around here, just accepted but it doesn't let me upvote as I don't have enough reputation points apparently. Thanks again! – Sean Dec 31 '21 at 22:19
0

I had to modify php configuration through a php.ini file. By default, this setting was turned off on my new server.

zlib.output_compression = On
Sean
  • 3
  • 3