Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 4 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
[vBulletin 4] Simple way of including an external PHP file
Crimm's Avatar
Crimm
Join Date: Feb 2007
Posts: 170

 

Show Printable Version Email this Page Subscription
Crimm Crimm is offline 05-12-2010, 10:00 PM

There are other articles out there on variables, templates, etc on vBulletin 4. This is a simple example of including an external PHP files like you used to be able to do here:

http://www.vbulletin.com/forum/showt...P-or-HTML-File

Thanks to this Blog post by David IB http://www.vbulletin.com/forum/entry...s-to-templates and this article by cellarius https://vborg.vbsupport.ru/showthread.php?t=228078

I have figured out it's only a simple extra step.

Step 1: Create a new plugin
  • Hook Location: What area of the forums you want this variable to appear. Don't know where? Use global_start
  • Title: Give it a title
  • Execution order: Your choice
  • Plugin PHP Code:

    Code:
    ob_start();
      require_once('LOCATION OF EXTERNAL FILE');
      $php_include = ob_get_contents();
    ob_end_clean();
    vB_Template::preRegister('TEMPLATE YOU ARE USING',array('php_include' => $php_include));

Step 2: You will have to figure out these two entries for yourself: LOCATION OF EXTERNAL FILE & Hook Location

To give you an example of what you should use is that if you want to display your external PHP file on your Forum's Home. Then replace these two with these values:

Hook Location with forumhome_start
TEMPLATE YOU ARE USING with FORUMHOME

Keep in mind that global_start will still be acceptable, but it's extra loading time where it's not needed. Therefore choosing the optimum hook location is better for your performance overall.

Step 3: Visit the Style Manager -> TEMPLATE YOU ARE USING and place the variable in your style where you want it. You will have to use the new format.

Code:
{vb:raw php_include}
That's it - Pretty simple; see?

Notes, If you want to:

Include this PHP file in multiple templates then preRegister it for the multiple templates:

Code:
vB_Template::preRegister('TEMPLATE YOU ARE USING',array('php_include' => $php_include));
vB_Template::preRegister('TEMPLATE YOU ARE USING 2',array('php_include' => $php_include));
Thanks to David IB again.

I'm still learning as I go with vb4, but if I learn some more notes to add... I'll drop by here.

I hope that helps some one out there!
Reply With Quote
  #62  
Old 04-07-2011, 11:26 PM
BirdOPrey5's Avatar
BirdOPrey5 BirdOPrey5 is offline
Senior Member
 
Join Date: Jun 2008
Location: New York
Posts: 10,610
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What I'm not understanding is why you would make several different php files when you could do the same with a single plugin or really even just template conditionals?

Can you give an example of the HTML code you want displayed from a php file?
Reply With Quote
  #63  
Old 04-08-2011, 10:46 AM
karlm's Avatar
karlm karlm is offline
 
Join Date: Jul 2006
Location: England
Posts: 447
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

OK, I'm doing it this way because I'm not a PHP guru... I'm doing it in a way I can understand.

So what I've done now is in the 'manage ads' area of the ACP, i've enabled a 'last post only' ad-spot.

While testing, it shows only to admins (once it goes live, i'll change this to show only to guests instead).

The content of this ad-code is:

Code:
<style type="text/css">
<!--
#adspot {
    width: 358px;
    height: 268px;
    border: none;
}
-->
</style>

<table style="text-align: left; width: 100%;" border="0" cellpadding="4"
cellspacing="4">
<tbody>
<tr align="center">
<td style="vertical-align: middle; text-align: center;">

<!-- GAMETAP  // UNDER 'PLAY ARCADE GAMES HERE' FORUM-->
<vb:if condition="$GLOBALS['forumid'] == 63 OR $GLOBALS['forumid'] == 54 OR $GLOBALS['forumid'] == 56">
<iframe src="/ads/gaming/rotate.php" frameborder="0" align="center" name="adspot" id="adspot" marginheight="0"><p>Your browser does not support iframes.</p></iframe>  
</vb:if>


<!-- CHRISTIAN STORE random sizes  // UNDER 'Religion & spirituality' FORUM-->
<vb:if condition="$GLOBALS['forumid'] == 3">
<iframe src="/ads/religion/rotate.php" frameborder="0" align="center" name="adspot" id="adspot" marginheight="0"><p>Your browser does not support iframes.</p></iframe>  
</vb:if>


<!-- MUSIC STORE random sizes  // UNDER 'music' FORUM-->
<vb:if condition="$GLOBALS['forumid'] == 52">
<iframe src="/ads/music/rotate.php" frameborder="0" align="center" name="adspot" id="adspot" marginheight="0"><p>Your browser does not support iframes.</p></iframe>  
</vb:if>

<!-- SPORTS STORE random sizes  // UNDER 'sports' FORUM-->
<vb:if condition="$GLOBALS['forumid'] == 55">
<iframe src="/ads/sports/rotate.php" frameborder="0" align="center" name="adspot" id="adspot" marginheight="0"><p>Your browser does not support iframes.</p></iframe>  
</vb:if>
</td>
</tr>
<tr align="center">
<td style="vertical-align: top;"><small><small><span
style="font-weight: bold;">Please <a href="/register.php" target="_parent">register</a> or sign in to remove these
advertisements.</span></small></small><br>
</td>
</tr>
</tbody>
</table>
In each directory is a series of text files with affiliate codes and the rotator picks a random file each time a viewer reads a thread.

I'm sure I could somehow move the /rotate.php file into the parent directory and have it refer to different sub-directories according to what forumID is being viewed... I have no doubt that can be done.

But - I don't know how to. So I'm trying to keep things simple for myself.
Reply With Quote
  #64  
Old 04-09-2011, 12:32 AM
BirdOPrey5's Avatar
BirdOPrey5 BirdOPrey5 is offline
Senior Member
 
Join Date: Jun 2008
Location: New York
Posts: 10,610
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yeah I honestly can't see it getting any better than an IFRAME as you have it without making life A LOT for difficult.

In the unlikely event someone has a browser that doesn't allow IFRAMEs is there some static content you can put between the <IFRAME> and </IFRAME> tags besides "your browser does not support iframes" ? Maybe pick one of the codes you'll be randomizing so it's better than nothing?
Reply With Quote
  #65  
Old 04-10-2011, 01:07 PM
karlm's Avatar
karlm karlm is offline
 
Join Date: Jul 2006
Location: England
Posts: 447
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Just wanted to add, as I said previously, I'm not guru where coding is concerned... However, after a quick flick through the vbmanual, I've managed to condense my code using <vb:else if xyz /> statements and an additional <vb:else />. This means, now, it will check to see if viewer is in forum x,y,z and show appropriate php files - otherwise (if not in the above mentioned forums) it will show a google-adsense post instead.

WIN!
Reply With Quote
  #66  
Old 04-17-2011, 05:02 AM
iwpg iwpg is offline
 
Join Date: May 2008
Posts: 17
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I am planning on getting my entire site converted to take adbantage VB 4. I have more than 50,000 pages, so basically, I cannot use the global_start hook, since I would have hundreds of files / scripts loading on every page. This would not be good...

I thought about using global_start with one product (php master file), and then using conditionals within the script to decide which file to load. But decided to wait until I get some advice on this. One thing I did notice: If you have a php file loaded with global_start, every VB page may or may not use those variables. For example, I created a PHP script that looked for the last 5 forum posts, used a variable called $threadid. It happens to be that $threadid is also used within VB. So the last threadid that was called within my custom script forced VB to use that variable! All links on the forum loaded my own script's variable $threadid. Is this normal behavior? I did change my variable name to something unique, but I'm getting a little paranoid about this, because I may have more than this variable that is also used by VB.

This is what I plan... Any feedback would be great.

Product Example:

ob_start();
include('Masterfile.php');
$masterfile = ob_get_contents();
ob_end_clean();

vB_Template:reRegister('customfiletemplate',arra y('loadexternalscripts' => $masterfile));

----------------------------------

Masterfile.php Example:
PHP Code:
if ($_GET['file']=='aboutus'){
include_once 
"aboutus.html";
}
elseif (
$_GET['file']=='otherpage'){
include_once 
"otherpage.php";

----------------------------------
Template:
{vb: raw loadexternalscripts}

The files would then be loaded like customfilepage.php?file=aboutus and Mod-rewrite would do the trick with bringing basic filenames to the request.
Reply With Quote
  #67  
Old 04-17-2011, 03:42 PM
BirdOPrey5's Avatar
BirdOPrey5 BirdOPrey5 is offline
Senior Member
 
Join Date: Jun 2008
Location: New York
Posts: 10,610
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm pretty sure I've heard the global_start hook isn't called on CMS pages anyway. They were going to remove that hook but kept it to keep compatibility with older mods, but didn't add it to the "new" parts of VB4.

I really can't image trying to work on 50,000 pages... good luck with that.

But using the same variable names is a concern. Not only should you avoid the variable names used by vBulletin but also names other people have used in mods you have installed. When I make a mod I'll usually call my variables $bop_post or $bop_thread for example to know I'm not changing data used elsewhere.
Reply With Quote
  #68  
Old 04-18-2011, 02:56 AM
iwpg iwpg is offline
 
Join Date: May 2008
Posts: 17
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for the info Joe, do you think the method that I plan would work? This way, only one hook is created and loaded, waiting for $_GET instructions to fire off scripts.
Reply With Quote
  #69  
Old 04-18-2011, 03:25 AM
BirdOPrey5's Avatar
BirdOPrey5 BirdOPrey5 is offline
Senior Member
 
Join Date: Jun 2008
Location: New York
Posts: 10,610
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by iwpg View Post
Thanks for the info Joe, do you think the method that I plan would work? This way, only one hook is created and loaded, waiting for $_GET instructions to fire off scripts.
I'm really not understanding what you are really doing... surely you don't plan to write a php elseif statement with 50,000 options...
Reply With Quote
  #70  
Old 04-19-2011, 03:55 AM
iwpg iwpg is offline
 
Join Date: May 2008
Posts: 17
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I am using VB to include 1 PHP file that is open to requests. These requests load the appropriate files if that's the page being requested. So, this allows a hook to be placed in global_start without actually loading a script and consuming enormous amounts of system resources. The 50,000+ pages are generated from about 10 scripts/programs.

In other words, I am using this script as a hook on global_start with a custom template that has the vb raw variable.

This is my PHP code (Master.php). Seems to work very well so far.
PHP Code:
<?php
global $fnuser,$fnuserid,$msxtotal;
$csl $_GET['csl']; // Custom Script Location - Single Files with no directories. Mod rewrite on
    
if ($csl=='About-Us') {
    require 
"C/about.shtml";
    
$fglink "About Us";
    
$fgptitle "About Our Business";
    }
    elseif (
$csl=='Sitemap') {
    require 
"C/sitemap.php";
    
$fglink "Sitemap";
    
$fgptitle "Website Sitemap";
    }

mysql_select_db(vb);//Want to select back to VB database just in case another database was selected
?>
Included script on the hook (M.php):
PHP Code:
<?php

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################

define('THIS_SCRIPT''Master');
define('CSRF_PROTECTION'true);  
// change this depending on your filename

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array('CustomMaster',
);

// pre-cache templates used by specific actions
$actiontemplates = array();

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');

// ###### NOW YOUR TEMPLATE IS BEING RENDERED ######
$navbits construct_navbits(array('' => $fglink));
$navbar render_navbar_template($navbits);

$templater vB_Template::create('CustomMaster');
$templater->register_page_templates();
$templater->register('navbar'$navbar);
$templater->register('pagetitle'$fgptitle);
$templater->register('sidebarext'$sidebarext);
$templater->register('sidebaropen'$sidebaropen);  
print_output($templater->render());

?>
Mod-Rewrite Code:
Code:
RewriteRule ^C/([^/]+).php$ /FN/M.php?csl=$1 [QSA,L]
Then call it like M.php?csl=About-Us
Mod-Rewrite generates About-Us.php and you're done.
Reply With Quote
  #71  
Old 08-06-2011, 08:55 PM
angelcosta angelcosta is offline
 
Join Date: Nov 2010
Posts: 10
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you so freakin much!

The file get contents worked!
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:43 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04882 seconds
  • Memory Usage 2,355KB
  • Queries Executed 26 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (5)bbcode_code
  • (3)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (4)pagenav_pagelink
  • (11)post_thanks_box
  • (7)post_thanks_box_bit
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete