• Resolved KevinPlusPlus

    (@kevinplusplus)


    Hello,

    I have my WP Cron executed using PHP rather than the web server, and this causes the following warning in PHP 8.1:

    Undefined array key “SCRIPT_FILENAME” in /tablepress/classes/class-tablepress.php on line 110

    The problem is: ‘wp-login.php’ === basename( $_SERVER[‘SCRIPT_FILENAME’] )

    The code there already checks for DOING_CRON, but that check occurs AFTER the check against the _SERVER[‘SCRIPT_FILENAME’]

    This can be fixed simply by re-ordering the “or” conditions to check for CRON and XMLRPC first, and then for wp-login.php.

    Thanks,

    Kevin Hock

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    thanks for your post, and sorry for the trouble.

    That sounds strange. Why isn’t $_SERVER['SCRIPT_FILENAME'] set here? From everything I can see, it should also be set if a script is called via the CLI or a cron job?

    To be honest, I’m not a big fan of using reordering of if checks here, as it feels like this would only “hide” an actual underlying issue.

    Regards,
    Tobias

    Thread Starter KevinPlusPlus

    (@kevinplusplus)

    Tobias,

    I have been reading about this and it seems that SCRIPT_FILENAME may not always be set. For example: https://www.php.net/manual/en/reserved.variables.server.php

    Data: $_SERVER[‘SCRIPT_FILENAME’]
    Data type: String
    Purpose: The absolute pathname of the origin PHP file, including the filename
    Caveat: Not set on all PHP environments, may need setting by copying from __FILE__ before other files are included.

    I did some additional testing and this is unique to running from a cron job. If I run from the command line:

    php [full path removed for security]/wp-cron.php

    there is no warning. Run the same commandnd run from the cron job does generate the warning. It seems that this just a quirk of how the PHP job is spawned, perhaps because of running as root for the cron job?

    I don’t see any harm in re-arranging the order, the checks accomplish exactly the same thing and prevents a warning on this border case. PHP 8 is very strict about accessing non-existing array elements. If you are reluctant to re-order them, how about checking if the array index exists before accessing it, which is a better programming practice especially under PHP 8.

    ( isset($_SERVER[‘SCRIPT_FILENAME’]) && ‘wp-login.php’ === basename( $_SERVER[‘SCRIPT_FILENAME’] ) )

    Thanks,

    Kevin

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    thanks for looking into this some more! Really weird indeed.

    I would then lean towards that isset() check. That’s what WordPress Core also seems to be doing in 2-3 places.

    Thanks for bringing this up!

    Regards,
    Tobias

    Thread Starter KevinPlusPlus

    (@kevinplusplus)

    Tobias,

    Yes, it is a strange one. Thanks for your help!

    Kevin

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    sure, no problem!

    Best wishes,
    Tobias
    ?
    P.S.: In case you haven’t, please rate TablePress here in the plugin directory. Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Undefined array key “SCRIPT_FILENAME”’ is closed to new replies.