Web Hosting Talk







View Full Version : Navigation help....


kooshin
08-27-2005, 05:57 AM
Hello guys

I have a question. I have the following code

<?php
switch(php_navigation) {
default:
include('news.htm');
break; case"what1":
include('whatever1.htm');
break; case"what1":
include('whatever2.htm');
break; case"what1":
include('whatever3.htm');
break; case"what1":
include('whatever4.htm');
break; case"what1":
include('whatever5.htm');
}
?>

which creates urls like mydomain.com/index.php?page=what1 and it is a good way to have one main design for the site and include the rest of the pages as needed but the problem is whenever I add a link to the main page then I have to write up a new case and the file name and include etc which is a pain so I was wondering if there is a way to include pages automatically whenever I new link is made and the page is there instead of having to rewrite the above code.

Someone told me it is there and it is possible and that they once had a such code but not having it now . So I was wondering guys if there is any way to achieve such thing.

thanks in advance.

Regards;
Kooshin

X-TechMedia
08-27-2005, 08:52 AM
This checks to see if . is present in the pagename, to stop people setting the page as ../../password.file or whatever


if (!strstr($pagenav, '.')) {
if (!@include('path/to/folder/'.$pagenav.'.php')) {
include('path/to/folder/error.php');
}
}

Burhan
08-27-2005, 09:11 AM
Another way to do the above:


$default = 'main.php';
$pages = array('hello' => 'hello.php', 'home' => 'home.php', 'contact' => '/scripts/contact-us.php');

$page = isset($_GET['page']) ? trim($_GET['page']) : null;

if (in_array($page,$pages))
{
include_once $pages[$page];
} else {
include_once $default;
}


Now all you have to do is call the script like index.php?page=home

For any other page you generate, just add a new key/value pair to your $pages array.

kooshin
08-27-2005, 01:10 PM
Hey guys thank you both but you said "For any other page you generate, just add a new key/value pair to your $pages array." and that is what am avoding. I want to be able to create any new page put it in the directory it is in and when called like sitename.com/index.php?page=name it shows

that is how a friend of mine said he used to use it . So can't this be done?

Burhan
08-28-2005, 03:01 AM
This is a big security hole, I suggest you don't do it.

HalfBrian
08-28-2005, 04:26 AM
Although it is a security hole, you could do it like this:

<?php

$exten = ".html";

$split = explode("?", $HTTP_SERVER_VARS['REQUEST_URI']);

if(trim($split[1]) == "") {

$output = "/news" . $exten;

} else {

$split2 = explode(":", $split[1]);

if (count($split2) > 1) {

//Enter directory mode

$i = count($split2)-1;
$a = 0;

while($a < $i) {

$output .= "/" . $split2[$a];
$a++;

}

$output .= "/" . $split2[$a] . $exten;

} else {

$output = "/" . $split2[0] . $exten;

}
}

$output=str_replace("..", "", $output);
include("http://www.yourdomain.com" . $output);
?>

The form is this:

http://www.yourdomain.com/(thefileabove).php?(directory):(filename)

Soo:

If the file above is named index.php:

http://www.yourdomain.com/?php:tutorial
would point to
http://www.yourdomain.com/php/tutorial.html

yourdomain.com/?tutorial
Would point to
yourdomain.com/tutorial.html

And,

yourdomain.com/
would point to
yourdomain.com/news.html

It could also be used for multiple directories!

You should of course change the yourdomain.com include part (bottom of the script) and you COULD change the extension (top variable called $exten)

Hope this help,
Brian

kooshin
08-28-2005, 12:19 PM
Ok guys thanks. I didn't know that there was a security problem and I guess I will not do it. And I will probably see if there is a better way to do it with no security holes.

thanks