Jump to content

Dynamic PHP inclusion


Earpy

Recommended Posts

Well, I've made a dynamic PHP script which includes html files dynamicly and im wondering how to tweak it a little. The script currently only includes HTML files and im wondering how to edit it so that it will include both. So I want it to look at the parameter in page and search for the HTML, if that is not there, search for PHP, and if PHP is not there display the error page. Any solutions?

 

Thanks in advance for any help

 

 

 

[hide=Click here for code]<?php

 

if (isset($_GET['page']) && $_GET['page'] != "") {

 

$page = $_GET['page'];

 

if (file_exists('pages/'.$page.'.html')) {

 

@include ('pages/'.$page.'.html');

 

} elseif (!file_exists('pages/'.$page.'.html')) {

 

include_once('pages/error.html'); }

 

} else {

 

@include ('index.php?page=news.html');

 

}

 

?>

 

[/hide]

Link to comment
Share on other sites


<?php

if(isset($_GET['page']) && $_GET['page'] != "")

{

$page = $_GET['page'];

if(file_exists('pages/'.$page.'.html') || file_exists('pages/'.$page.'.php'))

{

include('pages/'.$page.'.html');

} 

else

{

include('pages/error.html'); 

}

} 

else 

{

include ('index.php?page=news.html');

}

?> 

 

 

 

If the PHP page exists, won't that script try and include the HTML page that won't exist? If I pass index.php?page=mypage and mypage.php exists, but mypage.html does not, this script will see mypage.php exists and then try to include mypage.html, which doesn't exist, so it needs a little work.

 

 

 

A note if this code is to be placed inside index.php itself :

 

The code is a bit inconsistent (not your fault since the OP makes it look like the code wouldn't be included in index.php but it probably will be), in the first instance you decided the variable "page" would be just the name of the file without extension. Further down in your error compensation code you've set page to "news.html" which would cause an endless loop. You also don't want to be including index.php within itself. We also need to declare the variable $page as equal to our GET.

 

 

 

@ OP, if this code is for your index.php page, then this may work a little better :

 

<?php

if(isset($_GET['page']) && $_GET['page'] != "")

{

$page = $_GET['page'];

if (file_exists('pages/'.$page.'.html')) 

{

	include ('pages/'.$page.'.html');

}

elseif (file_exists('pages/'.$page.'.php')) 

{

	include ('pages/'.$page.'.php');

}

else 

{

	include('pages/error.html');

}

}

else

{

include('pages/news.html');

}

?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.