Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 07-16-2006, 05:36 PM
Yorixz Yorixz is offline
 
Join Date: Jun 2005
Location: Netherlands
Posts: 284
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Display banner depending on browser setting?

Because of the requirements of one of my ad providers (50% of the impressions have to be to people from Canada, the USA or UK) I'm trying to figure out the best way to display these ads to the right visitors (and display alternative ads to the others).

I've noticed vBulletin allows (a few) PHP functions to be used in templates, sadly there is no function available to check for the browser language.

I've found the following code on php.net (and changed it a bit)
PHP Code:
function chooseLang($availableLangs){
   $pref=array();
   foreach(split(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]) as $lang) {
       if (preg_match('/^([a-z]+).*?(?:;q=([0-9.]+))?/i', $lang.';q=1.0', $split)) {
           $pref[sprintf("%f%d", $split[2], rand(0,9999))]=strtolower($split[1]);       
       }
   }
   krsort($pref);
   return array_shift(array_merge(array_intersect($pref, $availableLangs), $availableLangs));
}
 
if(chooseLang(array('en', 'sv', 'en', 'nl')) == 'en'){
echo 1;
} else {
echo 2;
} 
And I'm a bit wondering how to use this, I haven't got too much experience with plugins, I wouldn't know how to return a value to one of the templates so a little help or a pointing in the right direction would be very appreciated.

Yours,

Yorixz

Alright, I got a PM with some info about it on vB.com, but anyway, I managed to get the code working, it selected the right language, until I changed it some and now all it does is complaining that my field isn't found.
i'm using this code:
PHP Code:
function favoriteLang($availableLangs){
   $pref=array();
   foreach(split(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]) as $lang) {
       if (preg_match('/^([a-z]+).*?(?:;q=([0-9.]+))?/i', $lang.';q=1.0', $split)) {
           $pref[sprintf("%f%d", $split[2], rand(0,9999))]=strtolower($split[1]);       
       }
   }
   krsort($pref);
   return array_shift( array_merge( array_intersect( $pref, $availableLangs), $availableLangs) );
}

if ( !isset( $vbulletin->session->vars['adlang'] ) ) {
    $lang = favoriteLang( array( 'en', 'sv', 'nl', 'fr', 'pt', 'es', 'pl', 'no', 'de' ) );
    //$_SESSION['adlang'] = $lang;
    $vbulletin->session->db_fields['adlang'] = type_char;
    $vbulletin->session->set('adlang', $lang);  
    $adcode2 = 'session now set';
} else {
    $lang = $vbulletin->session->vars['adlang'];
    $adcode2 = 'session already set!';
}

switch($lang)
{
    case 'en':
    case 'fr':
        $adcode = 'adcode clicksor, ';
        break;
    default:
        $adcode = 'adremedy, ';
        break;
} 
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 08:54 AM.


Powered by vBulletin® Version 3.9.0
Copyright ©2000 - 2017, vBulletin Solutions Inc.