Jump to content

include code not working


younis

Recommended Posts

Hi,

 

This is fairly urgent, so I appreciate any help anybody can give me ASAP.

 

I recently upgraded from PHP 4 to 5 and now certain "include" code is not working on my site. Here's an example:

 

// if topic selected, display the relevant content.
if ($topic == 'page1') {
	include ("$topic.php");
	}
elseif ($topic == 'page2') {
	include ("$topic.php");
	}

// else wrong or no link selected.
else {
	print "Go to the <A HREF = 'main.php'>Main page</A>";
	}

 

Let's say the above code was in a PHP file called "frontpage.php". It used to be that if a person went to "mydomain.com/frontpage.php?topic=page1" then the page called for in the above code would include the content of "page1.php" in "frontpage.php".

 

It's no longer working. Any ideas why or how to fix it?

 

Any better way of doing this?

 

Thanks,

Steve

Link to comment
Share on other sites

To fix the code, you have to tell PHP where to find the value. Since it's coming from the URL, the value will be in the $_GET array:

<?php
// if topic selected, display the relevant content.
if ($_GET['topic'] == 'page1') {
	include ("{$_GET['topic']}.php");
	}
elseif ($topic == 'page2') {
	include ("{$_GET['topic']}.php");
	}

// else wrong or no link selected.
else {
	print "Go to the <A HREF = 'main.php'>Main page</A>";
	}
?>

 

But, I would re-write it as:

<?php
// if topic selected, display the relevant content.
$valid_pages = array('page1','page2');
if (in_array($_GET['topic'],$valid_pages)) {
include("{$_GET['topic']}.php");
}
// else wrong or no link selected.
else {
	print "Go to the <A HREF = 'main.php'>Main page</A>";
	}
?>

 

Ken

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.