Click to See Complete Forum and Search --> : Different include based on URL Variable
danpoulton
05-27-2007, 01:25 PM
Hi, does any one know of a script where i can have
whateverurl?page=whatever
and the page
<?php require_once ("$page.html"); ?>
thanks in advance :)
Charles
05-27-2007, 01:33 PM
<?php
extract ($_REQUEST);
include "$page.html";
?>or<?php include $_REQUEST['page'].'html' ?>
NogDog
05-27-2007, 01:41 PM
Something like this will help prevent users from successfully doing things like entering a URL of:
http://yoursite.com/page.php?.htaccess
<?php
$includeDirectory = $_SERVER['DOCUMENT_ROOT'].'/includes/';
$page = (!empty($_GET['page'])) ? basename($_GET['page']) : 'default.html';
if(is_readable($includeDirectory.$page))
{
include $includeDirectory.$page;
}
else // display default content
{
include $includeDirectory."default.html";
}
This assumes all the include files are in the "includes" directory (which of course you can change to whatever directory you want).
bokeh
05-27-2007, 03:38 PM
extract ($_REQUEST);Blimey!
Charles
05-27-2007, 04:38 PM
I was going for the least and most elegant.
danpoulton
05-28-2007, 04:39 AM
Thanks for all you help was just trying out the scripts. The
<?php include $_REQUEST['page'].'html' ?>
code works except the full stop
if i do http://www.gameslayer.co.uk/php/test.php?page=include
i get
Warning: main(includehtml): failed to open stream: No such file or directory in /home/gamesla/public_html/php/test.php on line 10
Warning: main(includehtml): failed to open stream: No such file or directory in /home/gamesla/public_html/php/test.php on line 10
Warning: main(): Failed opening 'includehtml' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/gamesla/public_html/php/test.php on line 10
As you can see its looking for includehtml
if i do http://www.gameslayer.co.uk/php/test.php?page=include.
it works any ideas how i can change the code to avoid this.
Sheldon
05-28-2007, 05:03 AM
you need to add the period for the file extention.
<?php include $_REQUEST['page'].'.html' ?>
But i would go with NogDog's safer version.
danpoulton
05-28-2007, 01:08 PM
Thanks for all your help i got it to work well.
Does anyone know how i could make the page containing the include take on the title of the include?
danpoulton
05-30-2007, 11:50 AM
How would you edit nog dog's safer code to automatically add .html to the included page?
Ignore my last post, i don't need to do that!
Thanks for all your help :)
danpoulton
05-30-2007, 12:00 PM
Sorry forgot to add this to last post. Will this code work from another server or domain and if not could it? As i run a cheats website it would be useful to offer my content over to other websites.
bokeh
05-30-2007, 12:26 PM
$page = (!empty($_GET['page'])) ? basename($_GET['page']) . '.html' : 'default.html';
This code will only work if the server has access to the file system so it will not work from a remote server.
danpoulton
05-30-2007, 12:34 PM
Thanks for the code and your quick reply.
This code will only work if the server has access to the file system so it will not work from a remote server.
Is there any way around that?
danpoulton
05-30-2007, 01:03 PM
for some of my pages the sub directory may be variable.
How could the code be adapted for
...&sd=folder&... in the URL
I'll only need to go down one sub directory.
I keep getting errors and am very new to PHP so any help is appreicated.
bokeh
05-30-2007, 01:18 PM
You can't do that with basename(). You'll need to look into other validation methods, for example checking the input doesn't contain two sequential periods, etc. (./../../)