Link to home
Start Free TrialLog in
Avatar of Brad Tenge
Brad TengeFlag for United States of America

asked on

PHP Includes Switch wont work on IIS and or Online

Hi, I have a real problem with using an array to include files dynamically with PHP. It is a very simple system but I cant get it to work when it is uploaded to my site (PHP is installed). I have been using XAMPP to develop with PHP and MySQL locally and this script works fine, when I click a link on the site nav it dynamically switches the included file. But when I upload the exact same script to my server which runs IIS instead of Apache (what I develop on locally, Apache is part of the XAMPP package) it does not include the default file (home.php) and when I click on a link in the nav nothing happens. It must have something to do with the version of PHP or the fact that it is running on IIS, but I cant know for sure and I can't find a solution.

//note that plain old includes worked like the header.php and the footer.php

If anyone can explain this to me, and help me make the script work it would be much appreciated. Thanks in advance!

--I attached some code--
//BELOW ARE THE FILES THAT ARE PART OF THE ISSUE WITH THE PHP INCLUDES SWITCH.
//index.php
<html>
<head>
<style type"text/css">
/*removed CSS code due to irrelevance*/
</style>
</head>
<body>
<div id="kungfoo">
 
 
    <div id="header">
        <?php include ("header.php"); ?>
    </div>
 
    <div id="main">
<?php 
    $pass = array('home','tipofthemonth','contact','video','store');
 
 
    if (in_array($_GET['id'], $pass)) {
     include ($_SERVER['DOCUMENT_ROOT'] . '/include/' . $_GET['id'] . '.php');
    }
 
 
    elseif (!isset($_GET['id'])) {
     include ($_SERVER['DOCUMENT_ROOT'] . '/home.php');
    }
 
 
    else {
 
      header("HTTP/1.0 404 Not Found");
}
?>
    </div>
	
	
	
    <div id="footer">
        <?php include ("footer.php"); ?>
    </div>
 
	
</div>
</body>
</html>
 
 
 
//header.php
<ul class="menu">
<li class="current"><a href="index.php?id=home" title="Home">Home</a></li>
<li><a href="index.php?id=video" title="Video">Video</a></li>
<li><a href="index.php?id=store" title="Store">Store</a></li>
<li><a href="index.php?id=tipofthemonth" title="Tip of the Month">Tip of the Month</a></li>
<li><a href="index.php?id=contact" title="Contact">Contact</a></li>
</ul>

Open in new window

Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

I would add these lines at the top of the script ...

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>

Also, issueing the header() in the middle of the HTML will fail as the the headers have already been sent to allow the html to be received at the browser.

header() must occur before any text output.

Avatar of TheAnarchist
TheAnarchist

$_SERVER['DOCUMENT_ROOT'] is not used in IIS. Thus, it has no value, and your include statement doesn't evaluate to anything useful.

You can find out where you currently are relative the document root by using $_SERVER['PHP_SELF'], and then working out how to get to the document root (for example, if you are two levels down from the document root and need to go two levels up to the document root, you would need ../../ at the start, and then have /include/ ).
<?php
phpinfo();
?>

will help you see what you've got available - near the bottom.
try using this in place of what you have


   if (in_array($_REQUEST['id'], $pass)) {
     include ($_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['id'] . '.php');
    }


    elseif (!$_REQUEST['id']) {
     include ($_SERVER['DOCUMENT_ROOT'] . '/home.php');
    }
sorry, try using this on instead:

<?php
    $pass = array('home','tipofthemonth','contact','video','store');


    if (in_array($_REQUEST['id'], $pass)) {
     include ($_SERVER['DOCUMENT_ROOT'] . '/' . $_REQUEST['id'] . '.php');
    }


    elseif (!$_REQUEST['id']) {
     include ($_SERVER['DOCUMENT_ROOT'] . '/home.php');
    }


    else {

      header("HTTP/1.0 404 Not Found");
}
?>
Avatar of Brad Tenge

ASKER

I have tried all of your suggestions but none seem to work. Can any one show or find a working example for me, I have looked but they all seem to have similar issues.
Okay, I got it to work, but not as well as I would have liked, I threw every thing in the root directory, and got rid of this $_SERVER['DOCUMENT_ROOT'] junk.
ASKER CERTIFIED SOLUTION
Avatar of Hube02
Hube02
Flag of United States of America 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 so much, this is such a useful function, I will sure get a lot of use out of this. Thank you very much!