[英]How to avoid user to open popup directly in the browser?
點擊按鈕,我正在調用一個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
我想要的是,當url包含mypopup_page.php
文件時, .htaccess
應該將其重定向到index.php,以便用戶永遠無法在瀏覽器中打開它,只能通過彈出窗口。
有沒有辦法通過.htaccess
文件這樣做,還是有更簡單的方法來完成我的任務?
最簡單的方法是使用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
}
父頁面:
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.