Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 10-02-2011, 07:57 AM
MoMan MoMan is offline
 
Join Date: Oct 2005
Location: USA
Posts: 301
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Integrating external script with vB

Hi,

I currently integrate my homepage with vbulletin in order to fetch user login and session data. The code I use to do that is as follows:

PHP Code:
// Integrate with vbulletin if requested
    
if (defined('VB_CONNECT'))
    {
        
define('DISABLE_HOOKS'true);
        
define('LOCATION_BYPASS'1);
        
chdir(VB_DIR);
        require_once(
'global.php');
        
chdir(BASE_DIR);
    } 
...and it works like a charm. However, when I wrap that in a function, like so:

PHP Code:
function logIn()
{
    
// Integrate with vbulletin if requested
    
if (defined('VB_CONNECT'))
    {
        
define('DISABLE_HOOKS'true);
        
define('LOCATION_BYPASS'1);
        
chdir(VB_DIR);
        require_once(
'global.php');
        
chdir(BASE_DIR);
    }
}

logIn(); 
Everything mysteriously blows up and I get the following PHP error:

PHP Fatal error: Call to a member function query_first_slave() on a non-object in [DIR]/forums/includes/functions.php on line 1386

Where line 1386 uses $vbulletin->db->query_first_slave to fetch the userinfo.

Any ideas why this could be? Is it a scope issue?

As a temporary workaround, I've simply placed the code in a separate file and include it whenever I need the integration. I know that a better solution would be to connect to the database only to fetch user data and use my own session, but for now I want to keep things quick and dirty (and secure)
Reply With Quote
  #2  
Old 10-02-2011, 09:47 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yes, I think it's a scope issue. The vb code is intending to create some global variables but when you put the "require" in a function they're not global.

Edit: maybe if you could figure out which variables those are you could add "global" statements in your function before the require_once('global.php').
Reply With Quote
  #3  
Old 10-02-2011, 05:49 PM
MoMan MoMan is offline
 
Join Date: Oct 2005
Location: USA
Posts: 301
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Though so - but I can't explain it! I tried making everything that ended up in $GLOBALS after the first code ran global inside the function, but that didn't help either (there were dozens of variables!).

Thanks for the reply- I suppose that instead of calling a function I will just include the code from a while while I'm in the global scope.
Reply With Quote
  #4  
Old 10-03-2011, 12:07 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hmm...I was curious about this (I'm not really any kind of php expert so I like to figure these things out). I found that if I just put a "global $vbulletin;" at the beginning of the login function it seems to work OK. I guess most of the globals aren't used for what you're doing (unless maybe it's because I don't have all you code? Anyway, the error you posted above goes away).
Reply With Quote
  #5  
Old 10-04-2011, 02:57 AM
MoMan MoMan is offline
 
Join Date: Oct 2005
Location: USA
Posts: 301
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Let me try again- I could have sworn I tried that, though!

Also, if you want to use functions as if they were in the global scope, here's a nice line of code that does just that (I use it for template evaluations so you don't have to write out the eval() statement every time:

PHP Code:
extract($GLOBALSEXTR_REFS); 
--------------- Added [DATE]1317700823[/DATE] at [TIME]1317700823[/TIME] ---------------

Update: no dice. Maybe my version to PHP is to blame (5.1.6)? Highly unlikely, but possible, I guess. I don't see reason from a coding standpoint that the inclusion from inside the function should work, as even with the variable scope vbulletin shouldn't care.
Reply With Quote
  #6  
Old 10-04-2011, 09:29 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

The extract thing is pretty handy, but of course it will only import the global variables that exist already - variables that are created after that line won't automatically be global, of course. And $vbulletin doesn't exist before global.php is included.
Reply With Quote
  #7  
Old 10-06-2011, 08:05 AM
souperman souperman is offline
 
Join Date: Mar 2011
Posts: 131
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I can tell you that it's not your php version. I had a similar problem with php 5.2.17, I fixed it by creating by own global file :P

Either way it's probably a scope issue like kh99 mentioned.
Reply With Quote
Reply

Thread Tools
Display Modes

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 06:19 AM.


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.04189 seconds
  • Memory Usage 2,219KB
  • Queries Executed 11 (?)
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
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)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
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete