PHP: include()

I’m migrating from GoDaddy and for some reason my website no longer works. It uses php include() pretty heavily to make life simpler. I tried adding a .htaccess with this added:

AddHandler application/x-httpd-php .html .htm .php

I also tried switching the PHP Mode in the Web Panel from 5.2 FastCGI to 5.3 CGI.

In my example below all that seems to show up no matter what page I try to load is the index.php with default.html showing.

I’m sure its a problem with the server as my code worked fine on another host.

In my index.php I try to include a page by using the url (“index.php?s=page1”) and this is the php code in index.php

<?php $doc_root = $_SERVER['DOCUMENT_ROOT']; switch ($s) { case "page1": include ("$doc_root/page1.php"); break; case "page2": include ("$doc_root/page2.php"); break; default: include("$doc_root/default.html"); break; } ?>


I also have inside each of the pageX.php files the ability to include html so that I can have urls like (“index.php?s=page1&p=subpage1”).


<?php $doc_root = $_SERVER['DOCUMENT_ROOT']; switch ($p) { case "subpage1": include ("$doc_root/subpage1.html"); break; case "subpage2": include ("$doc_root/subpage2.html"); break; } ?>


In index.php, you’re depending on PHP’s deprecated register_globals feature, which is not enabled on our servers. Adding the following line to index.php will solve the problem:

You’ll need something analogous for $p as well.

Ah thanks that worked perfectly.

Is it still advised to use the full paths using:

$doc_root = $_SERVER[‘DOCUMENT_ROOT’]; ?

Also what PHP Mode should I be using? Theres at least four options and I’m not really sure what the difference is between them.


[quote]Is it still advised to use the full paths using:

$doc_root = $_SERVER[‘DOCUMENT_ROOT’]; ?[/quote]
Not necessary, and not recommended. The working directory (i.e, the directory from which paths will be calculated) defaults to the directory containing your PHP script.

For new applications, I’d recommend PHP 5.3 FastCGI. Here’s the scoop:

[list][] 5.2 vs. 5.3: Different versions of the PHP interpreter. 5.2 is nearing end-of-life, but not all PHP extensions (or applications!) are fully compatible with 5.3 yet, so we’ve got to support both for a while longer. 5.3 is considerably faster, and supports some easier ways to customize it, so there’s really no reason not to use it for a new application.
] CGI vs. FastCGI vs. mod_php (the latter only on VPS and dedicated servers): Different ways of running the PHP script. FastCGI works great for most applications, but a few will work better as CGI for various reasons, and mod_php will provide some performance benefits on VPS/dedicated for certain applications.

Thanks a lot for the quick responses they have been really helpful :slight_smile: