PDA

View Full Version : PHP include triggers HTTP cascade


NexDog
07-19-2010, 09:37 AM
I followed this:

http://www.vbulletin.com/forum/showthread.php?173937-How-to-Include-a-PHP-or-HTML-File

And created a Plugin with:

ob_start();
include('/path/to/forum/wordpress.php');
$includeVP = ob_get_contents();
ob_end_clean();


wordpress.php is a file that calls SimplePie and pulls a feed from our blog and displays titles of the last 5 blog posts in the footer.

This is the code:

<?php

// Make sure SimplePie is included. You may need to change this to match the location of simplepie.inc.
require_once('/home/httpd/vhosts/domain.com/httpdocs/include/simplepie.inc');
require_once('/home/httpd/vhosts/domain.com/httpdocs/include/shorten.inc');

// We'll process this feed with all of the default options.
$feed = new SimplePie();
$feed->set_feed_url(array('http://www.domain.com/forum/external.php?type=rss'));
//$feed->set_feed_url(array('http://www.domain.com/blog/category/status/feed/'));
$feed->init();
$feed->set_item_limit(5);

// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
$feed->handle_content_type();

// Let's begin our XHTML webpage code. The DOCTYPE is supposed to be the very first thing, so we'll keep it on the same line as the closing-PHP tag.

/*
Here, we'll loop through all of the items in the feed, and $item represents the current item in the loop.
*/
foreach ($feed->get_items() as $item):
?>
<tr>
<td align="left" width="13"><img src="../../../../images/site/thread.gif"></td>
<td align="left" width="100%"><a class="feeds" href="<?php echo $item->get_permalink(); ?>"><?php echo shorten($item->get_title(), 35); ?></a><br /></td>
</tr>
<?php endforeach; ?>


shorten.inc is some extra code that shortens the char limit of the displayed title to 35 characters.

Problem is when I refresh the page it spawns hundreds of httpd process in a loop that raises loads to 150+.

Anyone have any ideas to what I'm doing wrong?

I use teh same code to pull vbulletin titles to our blog footer and rest of the site, and wordpress titles to its own footer and other areas of the site. On VB it just goes ballistic. :)

--------------- Added 1279536057 at 1279536057 ---------------

Actually it was the vbulletin.php containing code that pulled titles from vBulletin that caused teh loop. I activated teh wordpress.php plugin and the server is fine. However it doesn't display anything.

Guest190829
07-19-2010, 12:33 PM
I don't know why I've seen people in the habit of manually capturing an external PHP output rather than just putting the PHP code in the hook:

1.) Adapt and add the following code to Hook: parse_templates


require_once('SimplePie/simplepie.inc');
$feed = new SimplePie();
$feed->set_feed_url('https://vborg.vbsupport.ru/external.php?type=RSS2');
$feed->init();

$feed->handle_content_type();

$feed_html = '';

foreach($feed->get_items() AS $item)
{
$feed_html .= '<p><a href="' . $item->get_permalink() .'">' . $item->get_title() . '</a></p>';

}
2.) Place variable $feed_html into the footer variable where you wish.

and voila! None of this nasty ob_start() and ob_end() stuff, it's unnecessary!

Adapt the code to your liking, I even used SimplePie for you. You'll want to edit the include director, the feed url, and probably make the formatting of $feed_html a little nicer.

kh99
07-19-2010, 01:08 PM
I don't know why I've seen people in the habit of manually capturing an external PHP output rather than just putting the PHP code in the hook:


Well, for one thing there's an example of how to do it in the vBulletin manual, and not everyone's a PHP programmer. But also if it's existing code or something obtained elsewhere, altering a copy of it can create a maintenance issue.

NexDog
07-19-2010, 01:12 PM
Hmm, also caused the server to crash. :)

Guest190829
07-19-2010, 01:24 PM
Well, for one thing there's an example of how to do it in the vBulletin manual, and not everyone's a PHP programmer. But also if it's existing code or something obtained elsewhere, altering a copy of it can create a maintenance issue.

Do you have a link to where it is in the manual ?

Hmm, also caused the server to crash. :)

I need more information to see what wrong. I ran this on my test server with no problems.

kh99
07-19-2010, 01:30 PM
Do you have a link to where it is in the manual ?


http://www.vbulletin.com/docs/html/main/templates_externalfiles

Just to be clear, I'm not saying that it's always the best thing to do just because it's in the manual, I'm just saying that's probably one reason you see it so much.

But I'm not a PHP expert - maybe you could elaborate on why you consider ob_start/ob_end "nasty".


ETA: BTW, my guess would be that the above is crashing because this line

require_once('SimplePie/simplepie.inc');

has the wrong path to the include file. But it's just a guess.

Guest190829
07-19-2010, 01:51 PM
http://www.vbulletin.com/docs/html/main/templates_externalfiles

Just to be clear, I'm not saying that it's always the best thing to do just because it's in the manual, I'm just saying that's probably one reason you see it so much.

But I'm not a PHP expert - maybe you could elaborate on why you consider ob_start/ob_end "nasty".


ETA: BTW, my guess would be that the above is crashing because this line

require_once('SimplePie/simplepie.inc');has the wrong path to the include file. But it's just a guess.

Well, in my opinion, it adds an additional (and often unnecessary) layer of overhead and muddles transparency.

I think forum owners are using it as a quick means to modify their forums without understanding exactly what they are doing and how it works.

The manual states "Including external files," but I am seeing people create external files just to use that generalized snippet of code.

kh99
07-19-2010, 01:59 PM
The manual states "Including external files," but I am seeing people create external files just to use that generalized snippet of code.

Ah, I see. That makes sense. Carry on. :)

NexDog
07-20-2010, 02:55 AM
I think I'll just give teh job to someone that understands vbulletin and php better than I (which is everyone over teh age of 3).

kh99
07-20-2010, 10:12 AM
I think I'll just give teh job to someone that understands vbulletin and php better than I (which is everyone over teh age of 3).

Well that's up to you of course, but don't let our side discussion discourage you. There's really nothing wrong with doing it either way, IMHO.

You could add some HTML comments to the output to see what's happening (e.g. is it not displaying because there's nothing for the foreach loop to do, or is it never getting there?).

Guest190829
07-22-2010, 12:24 AM
Yes, please don't mistake my query for insult, I was wondering why I saw this trend pop up recently and now that I know its in the manual, it's not surprising!