Question:
What is wrong with this PHP?
LeftWinger11
2009-12-18 19:43:09 UTC
I am trying to write a script that will change the content displayed in a page using the PHP include function and a switch case statement. The switch/case statement works fine but the pages will not be included. The script is trying to find the directory inside the current file. How can I make it so it will actually find the right location?

function display_page_content() {

$page = $_GET['page'];

switch($page) {

case 'page1':
include $_SERVER['DOCUMENT_ROOT'].
"/folder/subfolder/another/somefile.php";
break;

case 'page2':
include $_SERVER['DOCUMENT_ROOT'].
"/folder/subfolder/another/thisfile.php";
break;

case 'page3':
include $_SERVER['DOCUMENT_ROOT'].
"/folder/subfolder/another/thatfile.php";
break;

default:
require $_SERVER['DOCUMENT.ROOT'].
"/folder/subfolder/another/anotherfile.php";
}
}


PS. Each of the include statements should not be broken up into two lines. Yahoo Answers keeps cutting them off so imagine they are one line of code.
Three answers:
just "JR"
2009-12-19 00:02:15 UTC
You should not have the need to include $_SERVER[], but just the path to the filename.

This means you must know first in which folder you are to start with (usually the "root").

Then, your include files:

include ("filename.php"); // NO "/" in front, for files in the SAME folder as the calling script.

include ("subfolder/filename.php"); if they are in a sub-directory

include ("../filename.php"); if it is in a folder just above the current script

include ("../../filename.php"); if they are two folders above.
2009-12-18 20:36:05 UTC
Assuming that, from your web root, there's folder/subfolder/another, and the files actually are there, try:



case 'page1':

include "/folder/subfolder/another/somefile.php"…

break;



case 'page2':

include "/folder/subfolder/another/thisfile.php"…

break;



case 'page3':

include "/folder/subfolder/another/thatfile.php"…

break;



default:

require "/folder/subfolder/another/anotherfile.p…
timepath
2009-12-18 19:58:28 UTC
Is $_SERVER['DOCUMENT_ROOT'] defined properly?



Do the pages you're trying to include actually exist?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...