根据引用URL的查询字符串替换img src值

时间:2011-05-24 19:54:45

标签: php query-string referrer

我有一个Magento商店,我想根据引用的URL是否包含特定的查询字符串或查询字符串值(adnetwork = as)来替换img的src值,如果不是简单地将图像保留为它是。

这是否足够简单,我到处寻找答案,甚至在各种论坛上都无济于事。

到目前为止,这是我所拥有的,似乎正确的语法,只是Firebug报告它无法加载图像URL,它只是显示“”。路径是正确的并且已经过测试。

<?php

// Path for default image source
$logo = $this->getSkinUrl('images/logo.gif');

// Get the referrer url from the $_SERVER array, or false if it's empty
$referrer = empty($_SERVER['HTTP_REFERER']) ? false : $_SERVER['HTTP_REFERER'];

// If the referrer exists
if($referrer) {

    // parse the url for the query
    $query_str = parse_url($referrer, PHP_URL_QUERY);

    // If there's a query string
    if(!empty($query_str)) {
        // Parse it and put the array into $components
        parse_str($query_str, $components);

        // If the "adnetwork" component is set
        if(!empty($components['adnetwork'])) {
            // Set the $logo var to the adnetwork image source
            $logo = $this->getSkinUrl('images/logo-no-number.gif');
        }
    }
}

?>

<div class="header">

    <img class="shop24" src="<?php echo $this->getSkinUrl('images/shop-24.gif') ?>" alt="Shop Online 24 Hours a Day"/>

    <div class="shopping-bag"><?php echo $this->getChildHtml('topcart')?></div>

    <div id="fplogo"><a href="<?php echo $this->getUrl('') ?>" title="Sale Basins - Clickbasin.co.uk"><img src="<?php echo $logo; ?>" alt="Sale Basins - Clickbasin.co.uk" /></a></div>

    <img src="<?php echo $this->getSkinUrl('images/side_logo_promo.gif') ?>" alt="Promotion" class="side-logo-promo"/>

    <?php echo $this->getChildHtml('topMenu') ?>

    <?php //<?php echo $this->getLogoSrc() ?>

</div>

我希望你能帮助别人。感谢。

1 个答案:

答案 0 :(得分:0)

你不应该简单地回应$logo吗?您要将图片分配给$logo,但您从未将任何内容设置为$logo

相关问题