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

发布于 2024-11-09 15:39:40 字数 1836 浏览 0 评论 0原文

我有一个 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>

我希望你能帮助大家。谢谢。

I have a Magento store and I would like to replace the src value of an img based on whether the referring URL contains a specific querystring or querystring value (adnetwork=as) and if not simply leave the image as it is.

Is this simple enough to do, I've searched everywhere for the answer, and even asked on various forums to no avail.

Here's what I have so far, seems like the correct syntax, just Firebug reports that it is unable to load the image URL, and it simply shows "". The paths are correct and have been tested.

<?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>

I hope you can help guys. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少跟Wǒ拽 2024-11-16 15:39:40

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

Shouldn't you be simply echoing $logo? you are assigning the image to $logo but you never set anything to $logo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文