Forum Moderators: coopster

Message Too Old, No Replies

Dynamic web site

how to include subdirectories

         

CNibbana

5:45 pm on Apr 30, 2004 (gmt 0)

10+ Year Member



I have successfully setup a dynamic web site with a template page and using this code to include the content:

<?php
if(isset($_GET['page'])) {
include("{$page}.php");
}
else {include('home.php');
}
?>

My problem is trying to include pages within subdirectories. The code above assumes every included page is within the root directory as so:
[domain.com...]

The $_GET['page'] command only returns everything after ?page= to use within my include and none of the hierarchy above it. I need the information between the domain and the page name in a subdirectory:
[domain.com...]

I am guessing the best way to do this is parse the url so that everything between the domain name and?page= is entered into the include?: (I'm actually using a mod-rewrite to make?page=stuff to appear stuff.php, and it is confirmed working)

So my actual url would look as follows:
[domain.com...]

My test code, which does not work. You'll notice I need everything but the .htm. I know it's in the $temp line, because I can't get that line to work on it's own.

<?php
if(isset($_GET['page'])) {
$temp [1] = split('[\.]', $REQUEST_URI);
include("{$temp}\{$page}.php");
}
else {include('home.php');
}
?>

To summarize, all I really need is someone to help me get the $temp line to work so that it will return the url information so that current links will work: <a href="about.htm"> and subdirectories will work: <a href="subdirectory/widgets/stuff.htm">

Unless someone has done this before and knows an entirely easier way?

httpwebwitch

7:39 pm on Apr 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need a function in your template which is aware of the directory depth and can translate links. Then your template needs to wrap each of its links in that function to translate the path to the appropriate depth.

Here is the function. Don't put this in an external file, that defeats the purpose! put it right in your template. Pay attention to the $localdepth variable, you need to set that if your root files aren't at the domain level. (eg. www.mydomain/~mysite/index.htm)


function relativelink($url){
if (strstr($url,"http")){
return $url;
}else{
// CHANGE THIS VARIABLE WHEN THE SITE IS MOVED TO DIFFERENT DEPTHS
$localdepth=0;
global $HTTP_SERVER_VARS;
$heredepth=substr_count($HTTP_SERVER_VARS['SCRIPT_URL'],"/"); // counts number of slashes in URL
$newurl="";
for ($linkdepth=1;$linkdepth<($heredepth-$localdepth);$linkdepth++){
$newurl .= "../";
}
$newurl .= $url;
return $newurl;
}
}

You then need to use that wrapper everytime you are using a linked file or file path. For example:


<?php
include(relativelink('includes/config.php'));
?>

<img src='<?php print(relativelink('images/logo.gif'))?>'>

<a href='<?php print(relativelink('things/stuff/page.htm'))?>'> page </a>

See how it works - the function counts the number of slashes in the URL and adds as many "../" to the links as it needs. No more broken links!

Good luck,
httpwebwitch

CNibbana

10:11 pm on Apr 30, 2004 (gmt 0)

10+ Year Member



Thank-you for your help. I'll play around with this during the weekend.

BTW, anyone know how to make the $temp [1] = split('[\.]', $REQUEST_URI); function work also?

I'd like to try it both ways. Thanks.

isitreal

7:06 pm on May 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just out of curiousity, have you thought about just making the site fully dynamic, like yoursite.com/?section=widgets&page=2

then you can extract the information you need from the section part, use that get variable to assign the include path.

You can still use the mod_rewrite, you just have to add that to it.

WhosAWhata

9:20 pm on May 1, 2004 (gmt 0)

10+ Year Member



nice solution