-2

I am working on a web crawler in php , I got always this problem but I did not find a way to resolve it

Notice: Undefined variable: website_to_crawl in C:\xampp\htdocs\dousser\index.php on line 16

This is my second essay , there is no error shown but also nothing is shown

<?php 

$website_to_crawl= "http://php.net";
$all_links= array();


function get_links($url)

{
global $all_links;
$contents= @file_get_contents($url);
$regexp= "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
preg_match_all("/$regexp/siU", $contents, $matches);
$path_of_url= parse_url($url, PHP_URL_HOST);

if (strpos($url, "https://") == true)
{
$type= "https://";
}
else
{
$type= "http://";
}


$links_in_array= $matches[2];

foreach ($links_in_array as $link)

{

if (strpos($link, "#") !== false)
{
$link= substr($link,0, strpos($link, "#"));
}

if (substr($link, 0, 1) == ".")
{
$link= substr($link,1);
}

if (substr($link, 0, 7) == "http://") {
$link= $link;
}

else if (substr($link, 0, 8) == "https://") {
$link= $link;
}

else if (substr($link, 0, 2) == "//") {
$link= substr($link,2);
}



else if (substr($link, 0, 1) == "#") {
$link= $url;
}
else if (substr($link, 0, 7) == "mailto:") {
$link= "[" . $link . "]";
}
else if (substr($link, 0, 1) != "/") {
    $link= "$type" .$path_of_url. "/" . $link;
}
else 
{
$link= "$type" .$path_of_url.$link;
}


if (!in_array($link,$all_links))
{
array_push($all_links, $link);
}



}//ends foreach 

}//ends function get_links

get_links($website_to_crawl);

foreach ($all_links as $currentlink)
{
get_links($currentlink);
}

foreach ($all_links as $currentlink)
{

get_links($currentlink);
}

foreach ($all_links as $currentlink)
{
if ((strpos($currentlink, "www.php.net") !== FALSE) && (strpos($currentlink, "http", 4) == FALSE))
{
echo $currentlink . "<br>";
$linkscount[] += $currentlink;
}
}

$count= count($linkscount);

echo "<br><br>There are $count links found by the crawler";

?>

This variable is for $website_to_crawl web site to crawl I have checked wether it is http or https website I have tested that

Thanks in advance In this case I am working on a webcrawler using a website url

Ahmed C
  • 61
  • 2
  • 14

1 Answers1

0

You can use:

if (strpos($url, "https://") == true)

instead of

if (strpos($website_to_crawl, "https://") == true)
Inzamam Idrees
  • 1,955
  • 14
  • 28