homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Simple Content Switching with PHP Variables
What's the best method?

 5:29 pm on Aug 27, 2002 (gmt 0)


I can't get this to work for anything. I have a php menu- I want the links to fill the variable below on my main index page. This is how I have one link set up:

echo ("<li><a href='index.html?page=contact'>" . "Contact" . "</a></li>");

So the contact.html page should replace the $page.html place. I thought it might have something to do with register globals but any way I set it isn't working.

<div id="left">
<?php @ require_once ("$page.html"); ?>

The main goal is simply to feed these content pages into the variable from the php menu. Any suggestions?



 5:39 pm on Aug 27, 2002 (gmt 0)

Maybe I am being slow today but I dont understand. You want them to click on the link which will take them to a new page index.html?page=contact Right? And on that page you want it to dynamically create the require and display the page right?

If that is all right (I like to assume) have you checked to make sure you are actually getting the varible from the link? Try just printing out $page to make sure its working.


 5:47 pm on Aug 27, 2002 (gmt 0)

Sort of confusing the way this is stated....

Try this instead

<?php require($HTTP_GET_VARS['page'] . ".html"); ?>

If that doesn't work, try

<?php require("contact.html"); ?>

If that doesn't work, your path to the file is wrong.

Don't put in the @ until it works - you're just making your debugging harder.



 5:58 pm on Aug 27, 2002 (gmt 0)

index.html is a three column layout:

content menu information

I want the content <div> to contain a $variable that will contain the pages that are being sent via a php menu. In my profile you can see two columns, a menu in one and a search box in the other.

Basically, the menu links should simply tell the 'content' pages (like contact.html, clients.html) to fill the variable in the content <div>. Creating one template for the entire site.

The problem I'm having right this second is with this line:

<div id="left">
<?php @ require_once ("$page.html"); ?>

-->It clears everything under the latest news and info image (the two right-side columns). $page is suppose to be empty- but I tested just to make sure that if I made a page.html with some content:

<div id="left">
<?php @ require_once ("page.html"); ?>

It works. But I can't switch the content pages without the $var.
Make more sense I hope? It could be real easy, I'm just new to this-

Thanks for any insight into how this might work.


 5:59 pm on Aug 27, 2002 (gmt 0)

<div id="left">
<?php require ($HTTP_GET_VARS['page'] . ".html"); ?>

Really, I don't see why this wouldn't work. It causes:

Fatal error: Failed opening required '.html' (include_path='.;c:\php4\pear')

All of these files are in the same folder.


 7:00 pm on Aug 27, 2002 (gmt 0)

Could it be possibly due to the fact your trying to require the varible? Try to reasign $page to a different varible that way its defined before the require is ran for sure. Do like $page1 = $HTTP_GET_VARS['page']
Then try require("$page1.html"); See if that works.


 7:06 pm on Aug 27, 2002 (gmt 0)

did you try it this way?


no quotes.


 7:15 pm on Aug 27, 2002 (gmt 0)

All of the above. Let me see if I'm right on these accounts, and if not I may just have to take it another direction- but that urks me.

index.html is in the same folder as menu.php. menu.php is require_once in another <div> no problem.

menu.php (example)
echo ("<li><a href='index.html?page=contact'>" . "Site Map" . "</a></li>");

<div id="left">
<?php require($page.html); ?>

-->I tried each suggestion above- and get:
Fatal error: Failed opening required 'html' (include_path='.;c:\php4\pear') in

What am I missing?


 7:31 pm on Aug 27, 2002 (gmt 0)

does the contact.html page exist?

if it does and is at the root level then I think I have another idea.

Take a look at the documentation for include and require on php.net.

They reference the include_path [php.net].

This is probably the problem. You need to reference the file structure without relation to urls. For the web I would use.

$content = $DOCUMENT_ROOT . $page . ".html";

Take a look at user contributed notes for require() [php.net]

<added>sorry madcat, I misunderstood what you were doing originally. When I read this last post I had the ooooooooooh, I see.


 9:20 pm on Aug 27, 2002 (gmt 0)

Phew! Ok, here we go, just a couple things- by the way did I mention I was on a Windows machine;).

The menu works as it should...

<div id="left">
$pool = @include($_GET['page'] . ".html");
if (!$pool) { include("hello.html"); }

But, the W3C validator is checking against the menu.php page and therefore not validating.

If I don't supress the @include function above:
Warning: Failed opening '.html' for inclusion (include_path='.;c:\php4\pear') -->
appears only on the home page?


 10:41 pm on Aug 27, 2002 (gmt 0)

Look at your error message (this is why you must not suppress error messages until you have a stable working live app).

Warning: Failed opening '.html' for inclusion (include_path='.;c:\php4\pear') -->

Question: What file is it including?
Answer: .html

In other words, $_GET['page'] is undefined. Why? I think you've answered it:

appears only on the home page?

On the home page, you probably get there by putting in a domain name and there are no GET parameters. If you are going to use this method, you MUST have $_GET['page'] default to something VALID if it is not defined. Currently it defaults to "" so

require($_GET['page'] . ".html");

defaults to



Two problems:
1. This will cause a parse error since "." is not an allowable character in a variable name.

2. Even if it PHP could parse this, it wouldn't give the desired result because it would be looking for the variable named $page1.html and it would not be taking the value of $page1 and [i]concatenating[/] it to the string ".html" and, even if it did, the . would then be the contatenation operator, so you would be concatenating the value of $page1 to "html" (no dot) so you would be looking for a file with a name of the form "filenamehtml" which is probably not what you want.


 2:43 am on Aug 28, 2002 (gmt 0)

Ok, have to find another way.


 3:04 am on Aug 28, 2002 (gmt 0)

$page . ".html" == "$page.html" != $page.html

If you just type in the url, hello.html will display. It's not until the links are chosen that hello.html will be replaced. So there is never "", no?

Something else is still wrong, because your error message says that it is trying to include ".html" which indicates that $page == "". Or was the error message from before you made that change? Failed to reload?

Anyway, it works now right?



 3:08 am on Aug 28, 2002 (gmt 0)

Nevermind- it is happening again on my hosts;O)


 3:11 am on Aug 28, 2002 (gmt 0)

Your right about that Tom. I just have to find a way to rid myself of this message on the home page:

Warning: Failed opening '.html' for inclusion (include_path='')


 7:20 am on Aug 28, 2002 (gmt 0)

If there is no value for $page you can set it to index.

if(!isset($page)) $page = "index";


if (empty($page)) $page = "index";

and then do the standard content display and it will load /index.html.


 2:10 pm on Aug 28, 2002 (gmt 0)

Thanks for your help. I'm going to have to try a new method- as it is pointed out that this code offers major security risks.

Back to the drawing board. I don't thing switching files to and fro a variable should be this tough;)


 2:32 pm on Aug 28, 2002 (gmt 0)

What is the security issues involed?

You could do if else statements

if ($page==Contact){
include ('contact.html');
} elseif ($page==Clients) {
include ('clients.html);
} else {
include ('index.html');

This way no matter what happens they will get a page, if they try and make stuff up they get index, since that if statement is not in their.


 4:57 pm on Aug 28, 2002 (gmt 0)

What is the security issues involed?

He's using a GET param to specify which file gets loaded. If someone were to get a malicious script on his server and set the page to include it, he has taken as yet no steps to stop this. But if they can upload a malicious script, they can probably get it to run some other way.

This can be made quite safe and is a common method for doing this. I don't think there's a real problem as long as you're aware of what's happening.

I don't thing switching files to and fro a variable should be this tough

It isn't. There's something that's not coming through from your explanations.



 5:01 pm on Aug 28, 2002 (gmt 0)

I was thinking if they can upload the script why not do the damage while uploading, but then again it all depends on how its all set up I guess. Thanks for answering.

I still think its not passing the variable for some reason.


 10:28 pm on Aug 28, 2002 (gmt 0)

Sorry, been away.

>>include($_GET['page'] . ".html");

completely unsafe depending on the configuration of PHP.
I could just use a url like [mydomain.com...]
In somedir there is an .htaccess file that enables php parsing for html files and so the included file could execute php code.



 10:39 pm on Aug 28, 2002 (gmt 0)

There isn't really too much else to say about the way I have things set up. I haven't learned enough about PHP to this point where I can fling around functions- this is kind of like taking stabs in the dark.

Simply, in my php.menu:

echo ("<li><a href='index.html?&amp;page=home'>" . "Home" . "</a></li>");
echo ("<li><a href='index.html?&amp;page=clients'>" . "Clients" . "</a></li>");
echo ("<li><a href='index.html?&amp;page=contact'>" . "Contact" . "</a></li>");
echo ("<li><a href='index.html?&amp;page=sitemap'>" . "Site Map" . "</a></li>");

In my index.html:

<div id="left">
include $_GET['page'] . ".html";

That's it. The menu works when I click on the links because $_GET is fulfilled. But upon entering the site: Warning: Failed opening '.html' for inclusion (include_path='.;c:\php4\pear').

Of course, there are the security risks that I have to think of now.

if/else statements become unrealistic if you have more than five pages.

If this works somehow, I'll see web designing in a whole new light though. Designing one template, that's it. It's nothing new, but for me it means time on my side.

This has nothing to do with php.ini-dist?


 10:48 pm on Aug 28, 2002 (gmt 0)

bells and whistles started going off. Since its only happening when you first hit the page its because $page is not defined yet. So the only idea I have is to define $page as jatar said above. This will default it too index.html so then it has something to include. This is of course ignoring the security issues.


 11:06 pm on Aug 28, 2002 (gmt 0)

<div id="left">
$valid = true;
switch ($_GET['page']) {
case 'page1': break;
case 'page2': break;
case 'page3': break;
case 'page4' : break;
case 'page5' : break;
case 'page6' : break;
case 'page7' : break;

default: $valid = false; break;

if ($valid) include($_GET['page'] . '.html');
else include('home.html');

Here it is.


Now what I want to do is figure out a way to make it modular. I have to study up but if I can find a way to use a function to make this effect work for 250+ pages- production time can be cut in half. db's?

Thank you for all your inputs- most appreciated.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved