Forum Moderators: coopster
<?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?
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
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.