Jump to content

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
https://forums.phpfreaks.com/topic/234010-include-code-not-working/
Share on other sites

By assigning the variables the proper values from the $_GET/$_POST/$_COOKIE/$_SESSION arrays such as $topic = $_GET['topic'] in every script that uses values from one of the superglobal arrays.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.