include file in another directory alters links

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Post Reply
seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: include file in another directory alters links

Post by seandisanti » Mon Apr 22, 2013 10:16 am

Please show your code for the nav.php file

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: include file in another directory alters links

Post by seandisanti » Tue Apr 23, 2013 9:37 am

is the file doing the including in your articles folder? References are based on the original script, not the included files. One way around it is to use a getRoot() function. like so:

Code: Select all


function getRoot($uri = '') {
    if ($uri == '') {
        $uri = $_SERVER['PHP_SELF'];
    }
    $depth = substr_count($uri, '/') - 1;
    $root = '';
    for ($x = 1; $x <= $depth; $x++)
        $root = '../' . $root;
    return $root;
}
 
and then just use that to make sure that your relative paths are relative to the root, rather than the script calling them. Here is an example of it in use for my autoload()

Code: Select all


function __autoload($name) {
    $root = getRoot();
    $theClass = $root . '_includes/classes/' . $name . '.php';
    if (file_exists($theClass)) {
        include($theClass);
    } else {
        die('Tried to load non-existent class: ' . $theClass);
    }
}
 

seandisanti
php-forum Fan User
php-forum Fan User
Posts: 973
Joined: Mon Oct 01, 2012 12:32 pm

Re: include file in another directory alters links

Post by seandisanti » Thu Apr 25, 2013 7:15 am

the '/' before the file name does the same as getRoot() if you're only running one domain on the server.

Post Reply

Who is online

Users browsing this forum: Baidu [Spider] and 5 guests