简体   繁体   English

如何隐藏实际链接,但不向联盟会员警告该链接来自重定向的链接?

[英]How to hide actual link but not alert the affiliate that the link came from a redirected link?

So far I've tried a simple page that 301 php redirects using the id (domain.com/link/1234/). 到目前为止,我已经尝试了一个简单的页面,该页面使用id(domain.com/link/1234/)重定向了301 php。 Then I tried the same php redirect (forwards the id) to a php page uses id to et the uri to redirect using JavaScript (redirects using onload). 然后,我尝试了将相同的php重定向(转发给id)到一个使用id到uri的php页面,以使用JavaScript进行重定向(使用onload进行重定向)。

If I just click the link everything works fine. 如果我只单击链接,一切正常。 But redirecting receives a 404 from the affiliate company. 但是重定向从联属公司收到404。 Any ideas as to how I can accomplish this? 关于如何实现此目标的任何想法?

I would like to have all of my links use the id of the post. 我希望所有链接都使用帖子的ID。

example: domain.com/link/1234/ 例如:domain.com/link/1234/

instead of the affiliate link: affiliate.com/sniper.php?s=g&7=8&g=0 而不是会员链接:affiliate.com/sniper.php?s = g&7 = 8&g = 0

EDIT 编辑

So if I use this JavaScript in the head it redirects perfect. 因此,如果我在头部使用此JavaScript,它将完美重定向。

<script>
    var url = "http://affiliate.com/sniper.php?s=g&7=8&g=0";

    // IE8 and lower fix
    if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
    {
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }

    // All other browsers
    else { window.location.replace(url); }
</script>

But if I add PHP into the JavaScript to add the URL dynamically. 但是如果我将PHP添加到JavaScript中以动态添加URL。 For example: 例如:

<script>

    var url = "<?php echo $url_to_affiliate_website; ?>";

    // IE8 and lower fix
    if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
    {
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }

    // All other browsers
    else { window.location.replace(url); }
</script>

This fails, I have zero idea why. 这失败了,我不知道为什么。 PHP can be on this page but not after the script tag. PHP可以在此页面上,但不能在script标记之后。

Any ideas?? 有任何想法吗??

In case you have a redirect rule that rewrite /link/{number}/ to link.php?id={number} 如果您有一个重定向规则,将/ link / {number} /重写为link.php?id = {number}

<?php
//link.php

//Consider sanitize the input. (if it's number, use (int) casting for instance)
$linkid = $_GET['id'];

/*
 Get the affiliate url based on the link ID.
 If you're using DB or whatever.
*/
$url = get_affiliate_url($linkid);

header("Location: ".$url);
?>
  • Didn't test it. 没有测试。

Is this the code you have used? 这是您使用的代码吗? I'm wondering how you are going to use a php variable inside a js functions. 我想知道您将如何在js函数中使用php变量。

Instead you should echo the php variable as php. 相反,您应该将php变量作为php回显。

EX: 例如:

<script type="text/javascript">

        var url = "<?php echo $url_to_affiliate_website; ?>";

        // IE8 and lower fix
        if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
        {
            var referLink = document.createElement("a");
            referLink.href = url;
            document.body.appendChild(referLink);
            referLink.click();
        }

        // All other browsers
        else { window.location.replace(url); }
    </script>

I think this will help you unless i have misunderstood your requirement. 我认为这将对您有所帮助,除非我误解了您的要求。

It is the way you have tried to embed the PHP variable in the JavaScript. 这是您尝试将PHP变量嵌入JavaScript的方式。 You have to do either this: 您必须执行以下任一操作:

<?php
echo '<script type="text/javascript">
    var url = "' . $url_to_affiliate_website . '";
    ...
    </script>';
?>

Or this: 或这个:

// jump to the verbatim mode, aka not interpreted by the PHP parser
?>
<script type="text/javascript">
var url = "<?php echo $url_to_affiliate_website; ?>";
</script>
<?php // continue rest of the PHP code

Edit 编辑

I just re-read your post; 我只是重新阅读了您的帖子; I'm not sure if that JavaScript is actually embedded in your document or in an external file, but if it's embedded, the file that's doing the redirect will need to be called "index.php" not "index.html" in order for the PHP to be evaluated. 我不确定JavaScript是否实际嵌入文档或外部文件中,但是如果嵌入了JavaScript,则进行重定向的文件将需要称为“ index.php”而不是“ index.html”,以便要评估的PHP。 Likewise, if the JavaScript is in an external file, the JavaScript file instead of the HTML file will need a ".php" extension and be included in the page like so: 同样,如果JavaScript在外部文件中,则JavaScript文件而不是HTML文件将需要扩展名“ .php”并包含在页面中,如下所示:

<script src="./javascript-file.php"></script>

As for the destination page knowing that the page was redirected, they are likely examining the HTTP headers and seeing that the page was 301 permanently redirected from your page. 至于目标页面,知道该页面已被重定向,他们可能正在检查HTTP标头,并发现该页面已从您的页面永久重定向到301。 You may be able to prevent/remove those headers before redirecting to the affiliate. 在重定向到会员之前,您可以防止/删除这些标头。 I'd need to see more of you're code to be able to assist with that. 我需要查看更多的代码才能提供帮助。

Since you say that the PHP can't be after the script tag, I'm assuming that's a limitation of the hosting environment. 既然您说PHP不能放在script标签之后,所以我假设这是托管环境的限制。 Here's a solution that may work for you: 这是一个可能适合您的解决方案:

<div id="concealed" style="display:none;"><?php echo $url_to_affiliate_website; ?></div>

<script>
    var url = document.getElementById("concealed").innerHTML;

    // IE8 and lower fix
    if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
    {
        var referLink = document.createElement("a");
        referLink.href = url;
        document.body.appendChild(referLink);
        referLink.click();
    }

    // All other browsers
    else { window.location.replace(url); }
</script>
<?php
$url_to_affiliate_website = "http://affiliate.com/sniper.php?s=g&7=8&g=0"; 
?>

Are you sure the value your giving to $url_to_affiliate_website is the url you want ? 您确定给$ url_to_affiliate_website的值就是您想要的url吗? Try also to rewrite your code this way. 也尝试以这种方式重写您的代码。 and use window.location instead 并改用window.location

// IE8 and lower fix
if (navigator.userAgent.match(/MSIE\s(?!9.0)/))
{
    var referLink = document.createElement("a");
    referLink.href = "<?=$url_to_affiliate_website;?>"; // note the = after ?
    document.body.appendChild(referLink);
    referLink.click();
}

// All other browsers
else { 
window.location = url;
}

Also make sure you declaring the $url_to_affiliate_website before echoing it. 还要确保在回显之前声明$ url_to_affiliate_website。 And confirm if your file has a .php extension not .html 并确认您的文件是否具有.php扩展名而不是.html

Save your file as UTF-8, if you use Notepad++, click Encoding->Encode in UTF8 then save your file and exit. 将文件另存为UTF-8,如果使用Notepad ++,则单击“编码”->“在UTF8中编码”,然后保存文件并退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM