PDA

View Full Version : Simple problem but idk how to fix it


Dave80
10-03-2011, 08:17 PM
Hello

It's very simple to explain. I want to include my custom php header on my forum so i set a plugin with global_start with this code:
ob_start();
include('custom/header.php');
$custom_header = ob_get_contents();
ob_end_clean();
Then i call $custom_header in template. It is not working. I can see nothing so, temporarily, i'm using
$custom_header = include('custom/header.php');
It works but, as soon as i open a single <div></div> tag in header.php all my forum become totally bugged. Oh and i also tried to ob_start in my file with this in header.php:
<? ob_start('cache_page'); ?>
<div class="blablablablablablabla"></div>
<? ob_end_clean(); ?>
Again bug is served. Any idea to fix this simple problem?

Thank you!

kh99
10-03-2011, 09:58 PM
Maybe try this:

ob_start();
include('custom/header.php');
$custom_header = ob_get_contents();
ob_end_clean();
vB_Template::preRegister('template_name', array('custom_header' => $custom_header));


(of course change 'template_name' to whatever template your want to put your custom header in.

And then in the template:

{vb:raw custom_header}

Here's an article about that: https://vborg.vbsupport.ru/showthread.php?t=228078


I don't know why the other things you posted didn't work - I learned a couple things about php just trying to figure out your code. :o

Mooff
10-03-2011, 10:19 PM
Why are you using ob_start and ob_get_contents instead of file_get_contents or file?

I think it might be possible that you mess parts of the vbulletin php file up, if you use ob_end_clean in the wrong place? Not exactly sure if end_clean closed the last ob_start - or everything. And i do not think you need it if you use file() or file_get_contents() instead?

just realised you use a *.php file, then

$foo = include(YOURFILE); should have the desired effekt as long as your phpfile returns the desired value.

kh99
10-03-2011, 10:37 PM
I'm guessing the OP is using ob_start()/ob_end_clean() because the vb manual has that as an example of how to include an external file. I think the reason for doing that is if you have a php file that produces html (as opposed to returning it in a string) then with this method you don't have to make any changes to it.

Dave80
10-03-2011, 10:46 PM
I'm using the code published (https://www.vbulletin.com/forum/showthread.php/173937-How-to-Include-a-PHP-or-HTML-File?highlight=file%20html) (as guide) in vB forum so the code you are "trying to figure out" is not mine :) Anyway now it works.

Mooff i'm using ob_start just because it was written in vB forum and the thread has been recently linked by "vBulletin Technical Support Lead" so i thought that it was good.

edit: exactly kh99. There are php and js scripts in the included file

Thanks.

kh99
10-03-2011, 10:50 PM
I'm using the code published (https://www.vbulletin.com/forum/showthread.php/173937-How-to-Include-a-PHP-or-HTML-File?highlight=file%20html) (as guide) in vB forum so the code you are "trying to figure out" is not mine...

Yes, I recognized that code as the suggested way to include an external file. The code I was "trying to figure out" was the return value of an include file (which I forgot that you could do), the ob_start('cache_page') where it includes a callback function, and using the <? ... ?> short tags - all things I just didn't know without looking them up in the php manual, so I learned something.

Anyway, I'm glad you got it working.