如何避免用户直接在浏览器中打开弹出窗口?

时间:2014-04-19 03:07:33

标签: javascript php .htaccess

点击按钮,我正在调用一个javascript函数,它只是使用以下代码打开一个新窗口。

window.open("mypopup_page.php", "_blank", "height=400, width=550, status=yes, toolbar=no, menubar=no, location=no, addressbar=no, top=200, left=300");

我的问题是,即使我在window.open中写了location = no,用户也可以看到弹出的url,他可以将其粘贴到浏览器的地址栏中直接打开弹出窗口。我不希望用户从浏览器打开弹出窗口。

我使用.htaccess进行重定向。例如,为了处理错误404,我在那里有以下代码:

ErrorDocument 404 /404.php

我想要的是,当网址包含mypopup_page.php文件时,.htaccess应该将其重定向到index.php,以便用户永远无法在浏览器中打开它,只能通过弹出窗口。

有没有办法通过.htaccess文件这样做,还是有更简单的方法来完成我的任务?

2 个答案:

答案 0 :(得分:2)

最简单的方法是使用PHP并检查$_SERVER['HTTP_REFERER']。如果引荐来源不是您希望它们来自的地址,例如,直接从浏览器链接,您可以将其他地方重定向到其中,或者做任何您想做的事情。

实施例: mypopup_page.php

<?php
$target_url = "http://" . $_SERVER['SERVER_NAME']; //this returns 'myexample.com'

//comparing the two variables. I'm using strpos here, but you can compare it in any way you want.
if(strpos($_SERVER['HTTP_REFERER'], $target_url) !== FALSE) {
  //all is good
} else {
  header("Location: http://google.com"); //redirecting here, but it can be anything
}

答案 1 :(得分:0)

父页面:

var varToSend = 'someuniquevalue';
var win = window.open("mypopup_page.php", "_blank", "height=400, width=550, status=yes, toolbar=no, menubar=no, location=no, addressbar=no, top=200, left=300");
win.varToSend = varToSend;
//or you might need to use setTimeout?
window.setTimeout(function(){win.varToSend = varToSend;}, 2000);//2000 = 2 seconds or whatever it might take for the page to finish loading.

mypopup_page.php

window.setTimeout(function(){
    if(typeof varToSend !== 'undefined' && varToSend === 'someuniquevalue'){
        //we are ok
    }else{
        window.close();//may work
        //or
        window.location.href = 'http://example.com/index.php';
    }
}, 2000);//2000 = 2 seconds or whatever it might take for parent to send variable.

可能是非js版本。

<noscript>
    <meta http-equiv="refresh" content="0; url=http://example.com/index.php"><!-- 0 = seconds -->
</noscript>