Jump to content

Cannot redeclare class


Thomisback

Recommended Posts

Hi,

I'm having a problem with a class, im using foreach but if I put the include in the foreach I get an error. Also when I do get it to work, it keeps loading, loading & loading.

I hope someone can help me, my code:

<?php
/* You can put this in other file and just include it */
function OpenDb($hostname,$uid,$pwd,$dbname){
$link = @mysql_pconnect($hostname,$uid,$pwd);
if($link && mysql_select_db($dbname)){
return($link);
}
else{
return(FALSE);
}
}
?>
<?php
// It may take a whils to crawl a site ...
set_time_limit(10000);




function QueryIntoArray($query){
        settype($retval,"array");
        
$result= mysql_query($query);
        if(!$result){
print "Query Failed";
        }        
        for($i=0;$i<mysql_numrows($result);$i++){
                for($j=0;$j<mysql_num_fields($result);$j++){
                        $retval[$i][mysql_field_name($result,$j)] = mysql_result
($result,$i,mysql_field_name($result,$j));
                }//end inner loop
        }//end outer loop
return $retval;
}//end function
?>
<!--An Example How To Use The functions
To try it simple change the appropriate variable to your own database & tables
-->
<HTML>
<HEAD>
<TITLE>PHP Array Test</TITLE>
</HEAD>
<BODY BGCOLOR=WHITE>
<?php
OpenDb("xxx","phpcrawler","xxx","xxx") or die("Failed Opening Database");

$result = mysql_query("SELECT word FROM words");
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    foreach ($line as $col_value) {



// Inculde the phpcrawl-mainclass
include("classes/phpcrawler.class.php");

// Extend the class and override the handlePageData()-method


// Now, create an instance of the class, set the behaviour
// of the crawler (see class-reference for more methods)
// and start the crawling-process.

class MyCrawler extends PHPCrawler 
{
  function handlePageData(&$page_data) 
  {
    // Here comes your code.
    // Do whatever you want with the information given in the
    // array $page_data about a page or file that the crawler actually found.
    // See a complete list of elements the array will contain in the 
    // class-refenence.
    // This is just a simple example.
    
    // Print the URL of the actual requested page or file
    echo "Page requested: ".$page_data["url"]."<br>";
    
    // Print the first line of the header the server sent (HTTP-status)
    echo "Status: ".strtok($page_data["header"], "\n")."<br>";
    
    // Print the referer
    echo "Referer-page: ".$page_data["referer_url"]."<br>";
    
    // Print if the content was be recieved or not
    if ($page_data["received"]==true)
      echo "Content received: ".$page_data["bytes_received"]." bytes";
    else
      echo "Content not received";
    
    // ...
    
    // Now you should do something with the content of the actual
    // received page or file ($page_data[source]), we skip it in this example
    
    echo "<br><br>";
    echo str_pad(" ", 5000); // "Force flush", workaround
    flush();
  }
}



$crawler = &new MyCrawler();
$deurl = "crawlerwithget.php?link=$col_value";
// URL to crawl
$crawler->setURL($deurl);

// Only receive content of files with content-type "text/html"
// (regular expression, preg)
$crawler->addReceiveContentType("/text\/html/");

// Ignore links to pictures, dont even request pictures
// (preg_match)
$crawler->addNonFollowMatch("/.(jpg|gif|png)$/ i");

// Store and send cookie-data like a browser does
$crawler->setCookieHandling(true);

// Set the traffic-limit to 1 MB (in bytes,
// for testing we dont want to "suck" the whole site)
$crawler->setTrafficLimit(9999999999999999999 * 99999999999);

// Thats enough, now here we go
$crawler->go();


// At the end, after the process is finished, we print a short
// report (see method getReport() for more information)

$report = $crawler->getReport();

echo "Summary:<br>";
if ($report["traffic_limit_reached"]==true)
  echo "Traffic-limit reached <br>";
  
echo "Links followed: ".$report["links_followed"]."<br>";
echo "Files received: ".$report["files_received"]."<br>";
echo "Bytes received: ".$report["bytes_received"]."<br>";

    }
}
?>
</BODY>
</HTML>

 

Thanks a lot!

Link to comment
https://forums.phpfreaks.com/topic/105642-cannot-redeclare-class/
Share on other sites

see this?

 

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    foreach ($line as $col_value) {



// Inculde the phpcrawl-mainclass
include("classes/phpcrawler.class.php");

 

as you can see you have your class file in a loop

 

you only need it once so place it outside your loop

from a class you can create an object and putting the class file in a loop is pointless thast why you get the error cant redeclare class

see this?

 

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    foreach ($line as $col_value) {



// Inculde the phpcrawl-mainclass
include("classes/phpcrawler.class.php");

 

as you can see you have your class file in a loop

 

you only need it once so place it outside your loop

from a class you can create an object and putting the class file in a loop is pointless thast why you get the error cant redeclare class

 

Thanks but if I put it at the top of the page I still get an error :/

 

Fatal error: Call to undefined method stdClass::receivePage()

 

<?php
/* You can put this in other file and just include it */
function OpenDb($hostname,$uid,$pwd,$dbname){
$link = @mysql_pconnect($hostname,$uid,$pwd);
if($link && mysql_select_db($dbname)){
return($link);
}
else{
return(FALSE);
}
}
?>
<?php
// It may take a whils to crawl a site ...
set_time_limit(10000);

// Inculde the phpcrawl-mainclass
include("classes/phpcrawler.class.php");

// Extend the class and override the handlePageData()-method


// Now, create an instance of the class, set the behaviour
// of the crawler (see class-reference for more methods)
// and start the crawling-process.

class MyCrawler extends PHPCrawler 
{
  function handlePageData(&$page_data) 
  {
    // Here comes your code.
    // Do whatever you want with the information given in the
    // array $page_data about a page or file that the crawler actually found.
    // See a complete list of elements the array will contain in the 
    // class-refenence.
    // This is just a simple example.
    
    // Print the URL of the actual requested page or file
    echo "Page requested: ".$page_data["url"]."<br>";
    
    // Print the first line of the header the server sent (HTTP-status)
    echo "Status: ".strtok($page_data["header"], "\n")."<br>";
    
    // Print the referer
    echo "Referer-page: ".$page_data["referer_url"]."<br>";
    
    // Print if the content was be recieved or not
    if ($page_data["received"]==true)
      echo "Content received: ".$page_data["bytes_received"]." bytes";
    else
      echo "Content not received";
    
    // ...
    
    // Now you should do something with the content of the actual
    // received page or file ($page_data[source]), we skip it in this example
    
    echo "<br><br>";
    echo str_pad(" ", 5000); // "Force flush", workaround
    flush();
  }
}


function QueryIntoArray($query){
        settype($retval,"array");
        
$result= mysql_query($query);
        if(!$result){
print "Query Failed";
        }        
        for($i=0;$i<mysql_numrows($result);$i++){
                for($j=0;$j<mysql_num_fields($result);$j++){
                        $retval[$i][mysql_field_name($result,$j)] = mysql_result
($result,$i,mysql_field_name($result,$j));
                }//end inner loop
        }//end outer loop
return $retval;
}//end function
?>
<!--An Example How To Use The functions
To try it simple change the appropriate variable to your own database & tables
-->
<HTML>
<HEAD>
<TITLE>PHP Array Test</TITLE>
</HEAD>
<BODY BGCOLOR=WHITE>
<?php
OpenDb("p41mysql35.secureserver.net","phpcrawler","Kool4lifE","phpcrawler") or die("Failed Opening Database");

$result = mysql_query("SELECT word FROM words");
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    foreach ($line as $col_value) {







$crawler = &new MyCrawler();
$deurl = "http://www.rpgcrime.com/phpcrawl/crawlerwithget.php?link=$col_value";
// URL to crawl
$crawler->setURL($deurl);

// Only receive content of files with content-type "text/html"
// (regular expression, preg)
$crawler->addReceiveContentType("/text\/html/");

// Ignore links to pictures, dont even request pictures
// (preg_match)
$crawler->addNonFollowMatch("/.(jpg|gif|png)$/ i");

// Store and send cookie-data like a browser does
$crawler->setCookieHandling(true);

// Set the traffic-limit to 1 MB (in bytes,
// for testing we dont want to "suck" the whole site)
$crawler->setTrafficLimit(9999999999999999999 * 99999999999);

// Thats enough, now here we go
$crawler->go();


// At the end, after the process is finished, we print a short
// report (see method getReport() for more information)

$report = $crawler->getReport();

echo "Summary:<br>";
if ($report["traffic_limit_reached"]==true)
  echo "Traffic-limit reached <br>";
  
echo "Links followed: ".$report["links_followed"]."<br>";
echo "Files received: ".$report["files_received"]."<br>";
echo "Bytes received: ".$report["bytes_received"]."<br>";

    }
}
?>
</BODY>
</HTML>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.