[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1029
  • Last Modified:

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

0
btenge61
Asked:
btenge61
  • 3
  • 2
  • 2
  • +2
1 Solution
 
Richard QuadlingSenior Software DeveloperCommented:
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.

0
 
TheAnarchistCommented:
$_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/ ).
0
 
Richard QuadlingSenior Software DeveloperCommented:
<?php
phpinfo();
?>

will help you see what you've got available - near the bottom.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
l0ve2hat3Commented:
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');
    }
0
 
l0ve2hat3Commented:
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");
}
?>
0
 
btenge61IT SpecialistAuthor Commented:
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.
0
 
btenge61IT SpecialistAuthor Commented:
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.
0
 
Hube02Commented:
As stated by TheAnarchist, $_SERVER['DOCUMENT_ROOT'] does not exist on an IIS Server.

below is a function that will calculate the document root. It has been tested and modified for several different server configurations and so far it reliably returns the document root.

set a constant named DOCUMENT_ROOT and use this in place of $_SERVER['DOCUMENT_ROOT']. If you always use this you can safely move your scripts from server to server and be fairly confident that they will work.

I develop on an Apache Server and many times we end up deploying the sites on IIS.
<?php
  define('DOCUMENT_ROOT', GetDocumentRoot());
  
    function GetDocumentRoot() {
    // the purpose of this function is to define the website document root on any server.
    // $_SERVER['DOCUMENT_ROOT'] is not available on some web servers like IIS
    // when the global variable $_SERVER['DOCUMENT_ROOT'] is not available this function will
    // calculate the document root.
    // this single function makes the constant DOCUMENT_ROOT available on any server
    if (isset($_SERVER['DOCUMENT_ROOT'])) {
      $docroot = $_SERVER['DOCUMENT_ROOT'];    
    } else {      
      $filepath = $_SERVER['PHP_SELF'];
      
      if (isset($_SERVER['SCRIPT_FILENAME'])) {
        $absolutepath = $_SERVER['SCRIPT_FILENAME'];
      } elseif (isset($_SERVER['PATH_TRANSLATED'])) {
        $absolutepath = $_SERVER['PATH_TRANSLATED'];
      } else {
        die('Cannot Define DOCUMENT_ROOT in doc_root.php'); 
      }      
      // a fix for Windows slashes
      $absolutepath = str_replace("\\", "/", $absolutepath);      
      $docroot = substr($absolutepath, 0, strpos($absolutepath, $filepath));
    }      
    return $docroot;
  }
?>

Open in new window

0
 
btenge61IT SpecialistAuthor Commented:
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!
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now