0

I am following along a tutorial for a PHP web crawler and I keep getting an error concerning my indexing. PHP tutorial

ERROR: 
Notice: Undefined index: path in C:\wamp\www\crawler.php on line 19 
Call Stack
# Time   Memory  Function   Location
1 0.0010 260216  {main}( )  ..\crawler.php:0
2 0.0080 730552  crawl_site( )  ..\crawler.php:47
3 1.8320 2344408 perfect_url( ) ..\crawler.php:38

This is the function that is giving the error:

function rel2abs($rel, $base){
 if (parse_url($rel, PHP_URL_SCHEME) != ''){
  return $rel;
 }
 if ($rel[0]=='#' || $rel[0]=='?'){
  return $base.$rel;
 }
 extract(parse_url($base));
 $path = preg_replace('#/[^/]*$#', '', $path);
 if ($rel[0] == '/'){
  $path = '';
 }
 $abs = "$host$path/$rel";
 $re = array('#(/.?/)#', '#/(?!..)[^/]+/../#');
 for($n=1; $n>0;$abs=preg_replace($re,'/', $abs,-1,$n)){}
 $abs=str_replace("../","",$abs);
 return $scheme.'://'.$abs;
}

Line 19: $re = array('#(/.?/)#', '#/(?!..)[^/]+/../#');

Darren
  • 13,050
  • 4
  • 41
  • 79
Jesse
  • 213
  • 3
  • 13
  • probably a variable scope, by the looks of it, it's not even making its way there – Funk Forty Niner Aug 11 '15 at 02:11
  • 2
    It always amazes me how people can work with such messy code... Aside from that, post your full code with the line where the error occurs. The line you posted as 19, should not be causing the error. – RhapX Aug 11 '15 at 02:11
  • your `Line 19: $re = array('#(/.?/)#', '#/(?!..)[^/]+/../#');` does not have a `index: path` in it, so that is not line #19. – Sean Aug 11 '15 at 02:12
  • It isn't line 19 causing your error. You're doing this: `extract(parse_url($base));` which leads you to assume that `$path` gets extracted from `$base`. Yeah, **no**... do a simple `print_r($base);` and you'll see. – Darren Aug 11 '15 at 02:12
  • looking at your linked tutorial, it is probably caused by this line `if(($bp['path']!="/" && $bp['path']!="") || $bp['path']==''){` in `function perfect_url($u,$b){` – Sean Aug 11 '15 at 02:13
  • I am sorry, line 19 is: `$abs = "$host$path/$rel";` – Jesse Aug 11 '15 at 02:15
  • 1
    `$host` isn't even defined, in turn failing all other variables following it. – Funk Forty Niner Aug 11 '15 at 02:16
  • 1
    problem is that even with line #19 being `$abs = "$host$path/$rel";` you would be getting `Notice: Undefined variable: path` not `Notice: Undefined index: path` – Sean Aug 11 '15 at 02:17
  • 4
    I don't know if I should vote to close as unclear, or as undefined index. http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index – Funk Forty Niner Aug 11 '15 at 02:21
  • 1
    @Fred-ii- it looks like `$host`/`$path`/etc. are created/defined by `extract(parse_url($base));` ([`parse_url()`](http://php.net/manual/en/function.parse-url.php), [`extract()`](http://php.net/manual/en/function.extract.php)). I just learned something as well. – Sean Aug 11 '15 at 02:21
  • @Sean thanks Sean. Yet as you said above, the notice doesn't seem to support the code. – Funk Forty Niner Aug 11 '15 at 02:23
  • @Darren, although wouldn't that result in a `Notice: Undefined variable: path` not `Notice: Undefined index: path` when `$path` is called? since the OP never does `$base['path']` which would give the `Notice: Undefined index: path`? – Sean Aug 11 '15 at 02:53

0 Answers0