Help refactoring this PHP script

I have a script that looks up particular URLs in a table and returns the corresponding LName column value if it finds the URL fragment. The URL fragments are like iabforme.com, as shown in the sample below.

Lately, I’ve been getting a lot of these notices:
PHP Notice: Undefined offset: 0 in /srv/www/htdocs/files/getL.php on line 29

I guess since this is only a notice that the try…except does not fire. I re-enabled the $log parameter but am trying to figure out how to make this better code. Can anyone help me refactor it correctly?

Code for getL.php:

<?php
    require_once($_SERVER["DOCUMENT_ROOT"] . "/store/ajax/db_mysql.php");
    $log = false;
    $ip  = $_SERVER['REMOTE_ADDR'];
    // *****************************************************************************
    // Sample call to this script:
    // getLicensee.php?l_state=PA&l_url=http://www.iabforme.com/traini 
    // (only sending the first 30 chars or URL)
    // *****************************************************************************
    $state = trim($_GET["l_state"]);
    $l_url = (isset($_GET["l_url"])?trim($_GET["l_url"]):"");
    if($state != 'undefined' && strlen(trim($_GET["l_url"])) > 0) {
        $p_url = parse_url($l_url);
        try {
            $pp_url = explode('.', $p_url["host"]);
            array_shift($pp_url);
            $url = implode('.', $pp_url);
            $sql = "SELECT * from e_l where url = '".$url."' AND LState = '".$state."'";
            $st_results = Db::getInstance()->returnQuery($sql);
            if($log && !$st_results[0]) {
                // In case no results...
                $fp = fopen("/var/tmp/getL.txt", "a"); 
                fwrite($fp, str_repeat("*", 20)."getL_1".str_repeat("*", 20)."\n");
                fwrite($fp, "IP:".$ip."\n");
                fwrite($fp, "State:".$state."\n");
                fwrite($fp, "GET:".print_r($_GET, true)."\n");
                fwrite($fp, "Parsed URL:".print_r($p_url, true)."\n");
                fwrite($fp, "ShortURL:".$url."\n");
                fwrite($fp, "HOST:".print_r($pp_url, true)."\n");
                fwrite($fp, "RESULTS:".print_r($st_results, true)."\n");
                fwrite($fp, "SQL:".$sql."\n");
                fclose($fp);
                echo "a L.";
            } else {
                echo $st_results[0]["LName"];
            }
        } catch ( Exception $e ) {
            $fp = fopen("/var/tmp/getL.txt", "a"); 
            fwrite($fp, str_repeat("*", 20)."getL_2".str_repeat("*", 20)."\n");
            fwrite($fp, "IP:".$ip."\n");
            fwrite($fp, "State:".$state."\n");
            fwrite($fp, "GET:".print_r($_GET, true)."\n");
            fwrite($fp, "Parsed URL:".print_r($p_url, true)."\n");
            fwrite($fp, "ShortURL:".$url."\n");
            fwrite($fp, "HOST:".print_r($pp_url, true)."\n");
            fwrite($fp, "RESULTS:".print_r($st_results, true)."\n");
            fwrite($fp, "SQL:".$sql."\n");
            fclose($fp);
            echo "a L.";
        }
    } else {
        $fp = fopen("/var/tmp/getL.txt", "a"); 
        fwrite($fp, str_repeat("*", 20)."getL_0".str_repeat("*", 20)."\n");
        fwrite($fp, "GET:".print_r($_GET, true)."\n");
        fwrite($fp, "State:".$state."\n");
        fwrite($fp, "L_URL:".$l_url."\n");
        fwrite($fp, "IP:".$ip."\n");
        if(array_key_exists('HTTP_REFERER', $_SERVER)) {
            fwrite($fp, "REF:".$_SERVER["HTTP_REFERER"]."\n");
        } else {
            fwrite($fp, "REF: No Referer\n");
        }
        fclose($fp);
        echo "a L.";
    }
?>

Line 29 doesnt use an offset; line 35 does. Are you certain you’re using the correct version of the page?

Assuming line 35 is the actual culprit,

        if($log && !$st_results[0]) {
       } else {

In order for the Else to fire, either $log is False or $st_results[0] existed and contained a non-zero value.

As you had $log set to false, the Else will always fire; if $st_results came back with nothing, then this would cause the error; $log was false, but no results were returned.

AFA the line #s go, I added comments so the line with the error would change.

So this line?
echo $st_results[0]["LName"];

Maybe another conditional? i.e.
if($log && !$st_results[0]) {

Because of the && $log needs to be true.(I don’t see in your code where that might happen)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.