EDUSOHO踩坑笔记之五十三:Undefined offset故障排除

69 篇文章 5 订阅
59 篇文章 6 订阅

         EDUSOHO踩坑笔记之五十三:Undefined offset故障排除

          今天在跟踪EDUSOHO的程序时,发现下面的错误,导致服务器的数据库访问负荷飙高,以为是收到攻击了,后来排查下来发现不是,是因为一个访问边界没控制住,数组越界导致提取数据失败,不断重试导致的数据库访问飙高,像被攻击了一样。

[06-Nov-2020 09:04:03 Asia/Shanghai] PHP Notice:  Undefined offset: -1314340 in /mydata/wwwroot/edusoho/plugins/JsonRpcApiPlugin/Biz/JsonRpcApi/Service/Impl/JsonRpcApiServiceImpl.php on line 252
[06-Nov-2020 09:04:03 Asia/Shanghai] PHP Notice:  Undefined offset: -1302009 in /mydata/wwwroot/edusoho/plugins/JsonRpcApiPlugin/Biz/JsonRpcApi/Service/Impl/JsonRpcApiServiceImpl.php on line 252
[06-Nov-2020 09:04:03 Asia/Shanghai] PHP Notice:  Undefined offset: -1338698 in /mydata/wwwroot/edusoho/plugins/JsonRpcApiPlugin/Biz/JsonRpcApi/Service/Impl/JsonRpcApiServiceImpl.php on line 252
[06-Nov-2020 09:04:03 Asia/Shanghai] PHP Notice:  Undefined offset: -1322886 in /mydata/wwwroot/edusoho/plugins/JsonRpcApiPlugin/Biz/JsonRpcApi/Service/Impl/JsonRpcApiServiceImpl.php on line 252
[06-Nov-2020 09:04:03 Asia/Shanghai] PHP Notice:  Undefined offset: -1322887 in /mydata/wwwroot/edusoho/plugins/JsonRpcApiPlugin/Biz/JsonRpcApi/Service/Impl/JsonRpcApiServiceImpl.php on line 252
[06-Nov-2020 09:04:03 Asia/Shanghai] PHP Notice:  Undefined offset: -1302010 in /mydata/wwwroot/edusoho/plugins/JsonRpcApiPlugin/Biz/JsonRpcApi/Service/Impl/JsonRpcApiServiceImpl.php on line 252

排查下来的原因,在循环之前没有做list数据大小判断,并且第一个循环还是count()-1,如果为零时,又减一,导致负数组,就报此类错误

    public function getNextVideoTaskIdByTaskResultList($taskResultList)
     {

       if (count($taskResultList)>=1){      //前面加上列表记录必须至少有一个数据
         for ($i=(count($taskResultList)-1);$i<=count($taskResultList);$i--) {
            $task = $this->getTaskDao()->get($taskResultList[$i]['courseTaskId']);
            if (empty($task)) 
                continue;
             
            return $task['id'];            
        }
            
        }     //判断尾

    }

       这问题很常出现在数组中的,程序是能正确地运行下去,但是在日志中这样的报警:Notice: Undefined offset: ….. 如上面的代码示例,常规的做法,是把数组大小做个判断,这样就能正常处理数据了,不再报错了.
       问题是解决后offset:接下去的数字是出错的数组下标,一般是超出了数组的取值范围,如定义了数组$A[]有10个元数,如果出现了$A[10]就会出现错误(Notice: Undefined offset: 10 ….),因为数组的下标是从0开始的,所以这个数组的下标就只能是0~9.因此在出现这类问题时,不要急于用抑制显示的方法一定要注意你所用的数组下标,仔细思考一下,问题一定会很快得到解决的 !发也有可能是unset数组后再尝试读取其内容。

如有的网友写的下面的代码:

<?php# ...function get_match($regex, $content)  {  
    preg_match($regex,$content,$matches);     

    return $matches[1]; // ERROR HAPPENS HERE}

原因preg_match没找到匹配,$matches则为空数组。因此,正确的写法preg_match在访问之前检查是否找到匹配项$matches[0],如下:

function get_match($regex,$content){
    if (preg_match($regex,$content,$matches)) {
        return $matches[0];
    } else {
        return null;
    }}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jyl_sh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值