Link to home
Start Free TrialLog in
Avatar of catonthecouchproductions
catonthecouchproductionsFlag for United States of America

asked on

Create a dynamic array

I have the basic header/footer navigation using GET and putting file names in to an arrray:

<?php
if (isset($_GET['p'])) {
$pages = array('contact','drinksspecials','bulletin','pictures','media','designfriends');
if (in_array($_GET['p'], $pages)) { // if exists, then include it
$page=$_GET['p'] ;
}
else {
$page='404';
}
}

else { // default page
$page='default';
}

if ($page=='default') {
$header='header';
}
else {
$header='header-home';
}

include $header.".php";    
include $page.".php";
include "footer.php";

?>

I have alot of HTML files and I am wondering if it is possible to keep them as HTML and stil be called by the PHP? And also is there a way to create the array dynamically and just put in the filenames like above in the code?

Thanks

Ryan
Avatar of hernst42
hernst42
Flag of Germany image

you can use e.g. file_exists.

e.g:

if (!empty($_GET['p']) && file_exists(basename($_GET['p']) . '.php')) {
    $page = basename($_GET['p']);
} else if (empty($_GET['p'])) {
    $page = 'default';
} else {
   $page = '404';
}

the basename is needed to avoid remote inclusion of files like ?p=http://example.com/evil.script.txt
Avatar of catonthecouchproductions

ASKER

Will PHP allow me to keep the files as HTML? And I will change it to '.html'

Will that work?

So my code will be:



<?php
if (!empty($_GET['p']) && file_exists(basename($_GET['p']) . '.html')) {
    $page = basename($_GET['p']);
} else if (empty($_GET['p'])) {
    $page = 'default';
} else {
   $page = '404';
}

}

else { // default page
$page='default';
}

if ($page=='default') {
$header='header';
}
else {
$header='header-home';
}

include $header.".php";    
include $page.".php";
include "footer.php";

?>


Does that look correct?

Thanks,

Ryan
if you then include:

include $page.".html";

it looks ok to me. If you have php and html-files you must change the code so $page inculdes also the extension.
i missed that, so like you said.

Change all of the:

include $header.".php";    
include $page.".php";
include "footer.php";

to HTML?

Or can those still be PHP? Or would it be better practice to have all HTML?
I have:

<?php
if (!empty($_GET['p']) && file_exists(basename($_GET['p']) . '.htm')) {
    $page = basename($_GET['p']);
} else if (empty($_GET['p'])) {
    $page = 'default';
} else {
   $page = '404';
}

}

else { // default page
$page='default';
}

if ($page=='default') {
$header='header';
}
else {
$header='header';
}

include $header.".html";    
include $page.".htm";
include "footer.html";
?>

And when I test it, I get a blank page when I go to index.php
Any suggestions?

I checked my filenames, etc.
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you! Worked great!

Ryan