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;
}