php - PHP 代码中 undefined offset 错误

标签 php mysql loops curl implementation

我收到以下错误:

注意:未定义偏移量:1 在/home/content/53/7382753/html/Summer/wootsummer.php 第 101 行

我的代码位于 http://obsidianpunch.com/Summer

有问题的行是 $topnow=$top[$countforme]; 但是 $top 和 $countforme 已定义,所以我不确定为什么这会导致错误。非常欢迎任何建议。

对于上下文,它出现的代码片段 (wootsummer.php) 如下。

<html>
<body>

<?php

error_reporting(E_ALL);
set_time_limit (0);

$urls=explode("\n", $_POST['url']);
//$proxies=explode("\n", $_POST['proxy']);

$target=$_POST['target'];

$allurls=count($urls);
//$allproxies=count($proxies);

//use the new tool box
require "ToolBoxA4.php";
echo "before loops all ok";

for ( $counter = 0; $counter < $allurls; $counter++) {
//for ( $count = 0; $count <= $allproxies; $count++) {


 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL,$urls[$counter]);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'GET');
 curl_setopt ($ch, CURLOPT_HEADER, 1); 
 curl_exec ($ch); 
 $curl_scraped_page=curl_exec($ch); 

echo "after curl ok";

//call the new function parseA1
$arrOut = parseA1 ($curl_scraped_page);


$curl_scraped_page=strtolower($curl_scraped_page);
$haystack=$curl_scraped_page;
if (strlen(strstr($haystack,$target))>0) {



$FileName = abs(rand(0,100000));
$FileHandle = fopen($FileName, 'w') or die("can't open file");
fwrite($FileHandle, $curl_scraped_page);

echo "after fwrite ok";



$hostname="**************";
$username="************";
$password="******";
$dbname="**********";
$usertable="************";

$con=mysql_connect($hostname,$username, $password) or die ("<html><script language='JavaScript'>alert('Unable to connect to database! Please try again later.'),history.go(-1)</script></html>");
mysql_select_db($dbname ,$con);

$right = explode(",", $arrOut[0]);
$top = explode(",", $arrOut[1]);

for ( $countforme = 0; $countforme <= 5; $countforme++) {

$topnow=$top[$countforme];

$query = "INSERT INTO *********** (time, ad1) VALUES ('$FileName','$topnow')";
mysql_query($query) or die('Error, insert query failed');

echo "after mysql ok";

}

for ( $countforme = 0; $countforme <= 15; $countforme++) {

$rightnow = $right[$countforme];


$query = "INSERT INTO *********** (time, ad1) VALUES ('$FileName','$rightnow')";
mysql_query($query) or die('Error, insert query failed');

}

mysql_close($con);


echo '$FileNameSQL';


fclose($FileHandle);
}
curl_close($ch);

}



?>

</body>
</html>

上面引用的toolboxA4.php如下。

<?php

function strTrim ($strIn, $cutA, $cutB){
    //keeps what is between $cutA and $cutB
    $pieces = explode($cutA, $strIn, 2);
    $str1 = $pieces[1];  //keep everything after cutA 

    $pieces = explode($cutB, $str1, 2);
    $strOut = $pieces[0];  //keep everything before cutB            
    return $strOut;
}

function arrWords ($strIn, $theStart, $theEnd){
    //returns what is between $theStart and $theEnd
    $cutA = $theStart;
    $pieces = explode($cutA, $strIn);
    $pieces[0] = "";  //discard the first piece

    $cutB = $theEnd;
    foreach ($pieces as $key => $value) {
        $arrB = explode($cutB, $value, 2);
        $arrOut[$key] = $arrB[0];  //keep everything before cutB        
    }

    return $arrOut;
}

function arrElems ($strIn, $tag){
    //returns what is between <$tag> and </$tag>
    $cutA = "<$tag>";
    $pieces = explode($cutA, $strIn);
    $pieces[0] = "";  //discard the first piece

    $cutB = "</$tag>";
    foreach ($pieces as $key => $value) {
        $arrB = explode($cutB, $value, 2);
        $arrOut[$key] = $arrB[0];  //keep everything before cutB        
    }

    return $arrOut;
}

function arrElemAB ($strIn, $tagA, $tagB){
    //returns what is between <$tagA><$tagB> and </$tagB></$tagA>
    $cutA = "<$tagA><$tagB>";
    $pieces = explode($cutA, $strIn);
    $pieces[0] = "";  //discard the first piece

    $cutB = "</$tagB></$tagA>";
    foreach ($pieces as $key => $value) {
        $arrB = explode($cutB, $value, 2);
        $arrOut[$key] = $arrB[0];  //keep everything before cutB        
    }

    return $arrOut;
}

function DropTag ($strIn, $tag){
    //drops <$tag> and </$tag>.  $strIn can be a string or an array of strings.  $strOut is same type as $strIn.
    $strA = str_replace("<$tag>", "", $strIn);
    $strOut = str_replace("</$tag>", "", $strA);
    return $strOut;
}

function arrCompress ($arrIn){
    //deletes empty array entries
    if(!empty($arrIn)){
        foreach ($arrIn as $key => $value) {
            if ($value == "") {
                unset($arrIn[$key]);
            }       
        }
        $arrIn = array_values($arrIn);  
    }   
    return $arrIn;  
}

function arrDeDup ($arrIn){
    //changes duplicate array entries to empty string
    if(!empty($arrIn)){
        foreach ($arrIn as $key1 => $value1) {
            foreach ($arrIn as $key2 => $value2) {
                if ($key1 != $key2 and $value1 == $value2) {
                    $arrIn[$key2] = "";
                }   
            }
        }
    }   
    return $arrIn;  
}

function arrToString ($arrIn){
    //turns an array into a string
    $strOut = "";
    if(!empty($arrIn)){
        foreach ($arrIn as $value) {
            $strOut .= "$value<br>";                
        }
    }
    return $strOut; 
}

function arrContains ($arrIn, $strIn){
    //deletes array entries that do not contain $strIn.
    foreach ($arrIn as &$value) {
        if (stripos($value, $strIn) === false) {
            $value = "";
        }       
    }
    return $arrIn;
}

function arrNotContain ($arrIn, $strIn){
    //deletes array entries that contain $strIn.
    foreach ($arrIn as &$value) {
        if (stripos($value, $strIn) !== false) {
            $value = "";
        }       
    }
    return $arrIn;
}

function parseStrA1 ($strToParse){  
    //drop some tags
    $strA = DropTag ($strToParse, "b");

    //split the file into Top and RHS
    $strTOP = strTrim ($strA, "<body", "<div id=rhs_block");
    $strRHS = strTrim ($strA, "<div id=rhs_block", ">See your ad here");

    //get the elements using tags
    $arrTOP = arrElemAB ($strTOP, "span", "cite");
    $arrRHS = arrElems ($strRHS, "cite");  

    //remove empty elements
    $arrTOP = arrCompress ($arrTOP);
    $arrRHS = arrCompress ($arrRHS);

    //output results
    $strA = "TOP<br>" 
        . arrToString ($arrTOP) 
        . "<br><br>RHS<br>"
        . arrToString ($arrRHS);
    return $strA;
}

function parseA1 ($strToParse){ 
    //drop some tags
    $strA = DropTag ($strToParse, "b");

    //split the file into Top and RHS
    $strTOP = strTrim ($strA, "<body", "<div id=rhs_block");
    $strNAT = strTrim ($strA, "<body", "<div id=rhs_block");
    $strRHS = strTrim ($strA, "<div id=rhs_block", ">See your ad here");

    //get the elements using tags
    $arrTOP = arrElemAB ($strTOP, "span", "cite");
    $arrRHS = arrElems ($strRHS, "cite");

    //get the links
    $arrNAT = arrWords ($strNAT, '<a href="http://', '" ');
    $arrNAT = arrNotContain ($arrNAT, "oogle");

    //remove empty and duplicate elements
    $arrTOP = arrCompress ($arrTOP);
    $arrRHS = arrCompress ($arrRHS); 
    $arrNAT = arrDeDup ($arrNAT);
    $arrNAT = arrCompress ($arrNAT);

    //combine arrays into strings, comma separated values
    $arrOut[0] = implode(",", $arrRHS);
    $arrOut[1] = implode(",", $arrTOP);
    $arrOut[2] = implode(",", $arrNAT); 

    return $arrOut; 
}

?>      

最佳答案

问题是您正在从 0 到 5 迭代 $top 数组,而不知道该数组是否实际上包含这些索引处的元素。因此,快速解决方法是:

foreach($top as $topnow)
{
    $query = "INSERT INTO *********** (time, ad1) VALUES ('$FileName','$topnow')";
    mysql_query($query) or die('Error, insert query failed');

    echo "after mysql ok";
}

关于php - PHP 代码中 undefined offset 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6977881/

相关文章:

javascript - 在列表框中生成 "selected"html 标签

php - 将 SELECT 记录与 COUNT 结合起来

r - 如何在 R 环境中循环遍历编号数据帧。我必须在 R 中循环 22 个(可能是 22*6)数据帧

c - 将简单的字符标记放置在阵列板上以实现简单的糖果粉碎游戏

php - Laravel Eloquent 关系——相当于 MYSQL "join"

php - 警告 : mysqli_free_result() again

php - MySQL 选择时间戳在 X 时间之后的条目

MySQL 过程语法错误 "Missing END"

php - 选择框中选择的不同值和 id

java - 如何在java中通过两个循环访问外部变量