strpos():空针WordPress插件
Posted
技术标签:
【中文标题】strpos():空针WordPress插件【英文标题】:strpos(): Empty needle WordPress Plugin 【发布时间】:2016-02-23 17:10:00 【问题描述】:我刚刚完成了我的第一个插件的构建,并在我的个人网站上使用各种插件对其进行了测试,没有出现任何错误。然而,一些用户说该插件导致他们出现以下错误:
strpos(): 空针进入 /west/XXXXX/public_html/wp-content/plugins/bot-block/bot-plugin.php 上 第 200 行
在第 200 行我有这个:
//See if the domain that referred is in the current block url
$pos = strpos( $referrer, $site );
现在我看不出那行有问题,所以我会给你整个函数:
//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;
如果您需要查看完整的插件代码,我已将其放在 pastebin 此处:http://pastebin.com/gw7YbPVa
谁能帮我解决这个问题?
【问题讨论】:
你的参数是空的,所以它会抛出错误,你必须在使用它们之前调试这些变量以避免此类问题 【参考方案1】:快速解决方法是在尝试调用 strpos()
之前查看您的针头 ($site
) 是否为空。如果是空的,肯定大海捞针也找不着,直接跳过,把$pos
设为false。
$pos = strpos( $referrer, $site );
变成:
if ( $site == '' || !$site )
$pos = false;
else
$pos = strpos( $referrer, $site );
更好的解决方案是首先确定您的$site
变量为何为空。 $list
数组中的每个子元素是否包含另一个数组,而不是您期望的字符串?您可以在循环中使用var_dump( $site );
来查看该变量的内容。
【讨论】:
谢谢,我也很想知道为什么它是空的,但我无法复制这个问题:( @RuFFCuT 通常是这样。但至少通过这种逻辑,您可以检测并避免触发错误。 :以上是关于strpos():空针WordPress插件的主要内容,如果未能解决你的问题,请参考以下文章