openx中如何使用site-variable(参数)限制(关键词)广告显示

openx是个很强大的广告管理系统,可以根据不同的情况显示不同的广告。其中可以根据get参数值的不同来显示,即delivery limitation中的site-variable选项,name表示变量名,value表示值。比如在gender=male时显示男士用品广告,在 gender=female时显示女性用品广告。

但是实际使用时,发现不起作用,跟踪后发现,openx无法获得gender的值,原因在于url中的get变量没有传递给openx的js代码。于是在js代码中加入

document.write (’&gender=<?php echo $gender; ?>’);

有关信息可以参考:http://openxaddons.com/openx/targeting-ads-using-site-variable这种调用方法固然可以,但每次更改都需要变动代码,对开发者来说毫无意义,博主教你一种方法:

打开/openx/plugins/deliveryLimitations/Site/Variable.delivery.php,在末尾加入

function parse_query($var){
$var  = html_entity_decode(parse_url($var,PHP_URL_QUERY));
$var  = explode(‘&’, $var);
$arr  = array();
foreach($var as $val){
$x          = explode(‘=’, $val);
$arr[$x[0]] = $x[1];
}
unset($val, $x, $var);
return $arr;
}

 之后在请求前调用此方法

if (empty($aParams)) {
   //请求参数为空时调用_REQUEST参数
    $aParams = $_REQUEST;
}

 合并参数

$aParams=array_merge($aParams,parse_query($aParams['loc']));

 这样即可让openx获得get变量。不用去修改js。缺点就是rewrite成静态url的链接中没有get变量,此方法就无效,这时可采用其他限制条件。

值得注意时候请求参数如果含有中文的话,得注意编码问题,否则无法选择性的显示广告。

{php $keywords=iconv("GBK","UTF-8",$head_keywords);}

<?php

/*
+---------------------------------------------------------------------------+
| Revive Adserver                                                           |
| http://www.revive-adserver.com                                            |
|                                                                           |
| Copyright: See the COPYRIGHT.txt file.                                    |
| License: GPLv2 or later, see the LICENSE.txt file.                        |
+---------------------------------------------------------------------------+
*/

require_once MAX_PATH . '/lib/max/Delivery/limitations.delivery.php';

/**
 * Check if a value passed into the ad request (through $_REQUEST (so GET/POST/COOKIE)
 * via a name=value pair matches the limitation configured
 *
 * @param string $limitation The variable limitation
 * @param string $op The operator
 * @param array $aParams An array of additional parameters to be checked
 * @return boolean Whether this impression's channel passes this limitation's test.
 * @author     Chris Nutting <chris.nutting@openx.org>
 * @author Mohammed El-Hakim
 */
function MAX_checkSite_Variable($limitation, $op, $aParams = array()){
    if (empty($aParams)) {
        $aParams=$_REQUEST;
    }
    $aParams=array_merge($aParams,parse_query($aParams['loc']));
    $key   = substr($limitation, 0, strpos($limitation, '|'));
    $value = substr($limitation, strpos($limitation, '|')+1);
    if (!isset($limitation) || !isset($aParams[$key])) {
        // To be safe, unless the paramters passed in, and configured are avaiable,
        // return depending on if the $op is considered a 'positive' test
        return !MAX_limitationsIsOperatorPositive($op);
    } else if (MAX_limitationsIsOperatorNumeric($op)) {
        return MAX_limitationMatchNumeric($key, $value, $op, $aParams);
    } else {
            //如检测到未符合条件的广告,则无条件显示
            //@file_put_contents("/data/www/ad/var/cache/ddd.txt",var_export($op.strpos($aParams[$key],$value),true));
            if(strpos($aParams[$key],$value)===false)
                return !MAX_limitationsIsOperatorPositive("");
        return MAX_limitationsMatchString($key,$value,$op, $aParams);
    }
}
/**
* 解析变量地址访问页面前地
*/
function parse_query($var){
    $var=html_entity_decode(parse_url($var,PHP_URL_QUERY));
    $var=explode('&',$var);
    $arr=array();
    foreach($var as $val){
        $x=explode('=',$val);
        $arr[$x[0]]=$x[1];
    }
    unset($val, $x, $var);
    return $arr;
}
?>
原文地址:https://www.cnblogs.com/mengdejun/p/openx_site-variable_limitation.html