Skip to content
Advertisement

strpos(): Empty needle WordPress Plugin

I’ve just finished building my first plugin and have tested it with various plugins on my personal site with no errors. However some users are saying the plugin is causing the following errors for them:

strpos(): Empty needle in /west/XXXXX/public_html/wp-content/plugins/bot-block/bot-plugin.php on line 200

On line 200 I have this:

            //See if the domain that referred is in the current block url
            $pos = strpos( $referrer, $site );

Now I can’t see a problem with that line so I’ll give you the whole function:

//Check referrer function
function bot_block_parse()
{
    //Get the options for the plugin
    $options = get_option( 'bot_block' );

    //See if the request was from another site
    if( isset( $_SERVER['HTTP_REFERER'] ) )
    {
        //Split the URL into it's components
        $referrer = parse_url( $_SERVER['HTTP_REFERER'] );

        //Trim the components
        $referrer = array_map( 'trim', $referrer );

        //Get the domain name
        $referrer = $referrer['host'];

        //Get the block list
        $list = $this->create_block_list();

        //Loop through all the blocked domains
        foreach( $list as $site )
        {
            //Trim the domain
            $site = trim( $site );

            //Set the prefix for domains that aren't sub domains
            $prefix = 'www';

            //Split domain into smaller components
            $domainParts = explode( ".", $referrer );

            //See if the domain that referred is in the current block url
            $pos = strpos( $referrer, $site );

            //See if block subdomains is checked
            if( isset( $options['subdomains'] ) )
            {
                //Check to see if the domain was the current blocked site and if the prefix is not www
                if( $pos !== false && $domainParts[0] != $prefix )
                {
                    //Log spam
                    $this->log_spam( $site );

                    //Call the redirect function to see where to send the user
                    $this->bot_block_redirect();
                    exit;
                }
            }

            //See if the domain was the current site blocked and the prefix is www
            if( $pos !== false && $domainParts[0] == $prefix )
            {
                //Log spam
                $this->log_spam( $site );

                //Call the redirect function to see where to send the user
                $this->bot_block_redirect();
                exit;
            }
        }
    }
}

If you need to see the full plugin code I have put it on pastebin here: http://pastebin.com/gw7YbPVa

Can anybody help me figure this out please?

Advertisement

Answer

The quick fix is to see if your needle ($site) is empty before attempting to call strpos(). If it is empty, certainly it can’t be found in the haystack, so we should skip altogether and set $pos to false.

$pos = strpos( $referrer, $site );

Becomes:

if ( $site == '' || !$site ) {
   $pos = false;
} else {
   $pos = strpos( $referrer, $site );
}

The better solution is to determine why your $site variable is empty in the first place. Does each child element in $list array contain another array, instead of a string as you expect? You can use var_dump( $site ); in your loop to see the contents of that variable.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement