douxing8855 2014-04-19 03:07
浏览 18
已采纳

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

On the click of the button, I am calling a javascript function which just opens a new window using following code.

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

My problem is, even I have written location = no in window.open, user can see the url of the popup which he can copy paste into the addressbar of browser to open the popup directly. I don't want user to open the popup from the browser.

I am using .htaccess for redirection. For example to handle error 404, I have following code there:

ErrorDocument 404 /404.php

I want that, when the url contains mypopup_page.php file, .htaccess should redirect it to index.php so that user never be able to open it in the browser but only throught the popup.

Is there any way to do so through .htaccess file or is there any easier way to achieve my task?

  • 写回答

2条回答 默认 最新

  • dongqiuqiu4736 2014-04-19 03:14
    关注

    the simplest way is to use PHP and check $_SERVER['HTTP_REFERER']. If the referrer is not an address you want them to be coming from, eg, directly linking from browser, you can redirect them else where, or do whatever you want.

    EXAMPLE: 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条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改