I used the code found here for dynamic php breadcrumbs for my website. It works great! however, If I nest a folder more than 1 deep, It causes errors.
Here's the code that I have currently for the breadcrumbs.
<?php
function breadcrumbs($separator = ' » ', $home = 'Home') {
$path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
$base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
$breadcrumbs = Array("<a href=\"$base\">$home</a>");
$last = end(array_keys($path));
foreach ($path AS $x => $crumb) {
$title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
if ($x != $last)
$breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
else
$breadcrumbs[] = $title;
}
return implode($separator, $breadcrumbs);
}
?>
You are here: <?= breadcrumbs(' ♥ ') ?>
The easiest place to see a live example is here. If you click on the third link on the breadcrumbs, it ignores the second nested folder. I don't know enough PHP to trouble shoot the problem and how to fix it. I would think ideally it would watch for nested folders in the url.
You need to add previous crumbs to get it working correctly. Here's a fix:
<?php
function breadcrumbs($separator = ' » ', $home = 'Home') {
$path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
$base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
$breadcrumbs = Array("<a href=\"$base\">$home</a>");
$last = end(array_keys($path));
foreach ($path AS $x => $crumb) {
$title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
if ($x != $last)
$breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
else
$breadcrumbs[] = $title;
$base .= $crumb . '/';
}
return implode($separator, $breadcrumbs);
}
?>
You are here: <?= breadcrumbs(' ♥ ') ?>
that's because you're just linking to the current "crumb" $base$crumb
for you'll need to keep track of the path while building the links
foreach ($path AS $x => $crumb) {
$base .= $crumb.'/'; // <- keep adding crumbs to current path
$title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
if ($x != $last)
$breadcrumbs[] = "<a href=\"$base\">$title</a>"; //<- link to current path
else
$breadcrumbs[] = $title;
}
Assuming that your script runs fine , I see you have open_basedir restriction in effect
so you would have to ,if you're on apache to add this to your httpd.conf
<Directory /var/www/vhosts/domain.tld/httpdocs>
php_admin_value open_basedir none
</Directory>
If script causes problems try one of the answers already given