gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:29:50 25 4
gpt4 key购买 nike

单击按钮后,我将调用一个 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 文件来做到这一点,或者有没有更简单的方法来完成我的任务?

最佳答案

最简单的方法是使用 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
}

关于javascript - 如何避免用户直接在浏览器中打开弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com