使用php和HTTP_REFERER创建加载js的条件

发布于 2024-09-11 15:53:28 字数 1019 浏览 5 评论 0原文

我正在尝试在我正在处理的网站上创建启动页面效果(我知道启动页面很糟糕等,但我有我的理由),基本上我想仅在以下情况下调用运行启动图像覆盖的脚本访问者从外部网站访问该索引。这样,如果访问者从内部页面单击“主页”,则不会启动启动画面。我一直在搜索,似乎我可以使用 $_SERVER['HTTP_REFERER'] 用 php 来做到这一点,但我对 php 是全新的,在玩了整个下午之后似乎无法让它工作。

以下代码加载脚本,但它似乎并不关心引用 URL 是否包含“mysite”,并且页面顶部会出现一条错误消息:
“遇到 PHP 错误
严重性:通知
消息:未定义索引:HTTP_REFERER
文件名:..."

<?php
$referrer=$_SERVER['HTTP_REFERER'];
if(stristr($referrer, "mysite") == FALSE) {

echo '
<script type="text/javascript">
$(document).ready(function() {

                   $("#wrapper").hide();

        $("#imgContainer").npFullBgImg("/imgs/splash_image.jpg", {fadeInSpeed: 2000, center: true});   
        $("#logoContainer").fullLogoImg("/imgs/splash_logo.png", {fadeInSpeed: 2000, center: true});
        $("#logoContainer").click(function(){

                $("#wrapper").show("fast");
                $("#splash_kill").remove();
                $(this).remove();

        });
});
    </script>';
}
?>

任何帮助将不胜感激。谢谢!

I'm trying to create a splash page effect on a site I'm working on (I know splash pages are bad etc but I have my reasons), and basically I want to call the script that runs the splash image overlay only if the visitor is coming to the index from an external website. That way if a visitor clicks "home" from an internal page the splash won't launch. I've been searching about and it seems like I can do this with php using $_SERVER['HTTP_REFERER'], but I'm brand new to php and after playing with it all afternoon can't seem to make it work.

The following code loads the script but it doesn't seem to care if the referring URL contains "mysite" and an error message appears at the top of the page reading:
"A PHP Error was encountered
Severity: Notice
Message: Undefined index: HTTP_REFERER
Filename:..."

<?php
$referrer=$_SERVER['HTTP_REFERER'];
if(stristr($referrer, "mysite") == FALSE) {

echo '
<script type="text/javascript">
$(document).ready(function() {

                   $("#wrapper").hide();

        $("#imgContainer").npFullBgImg("/imgs/splash_image.jpg", {fadeInSpeed: 2000, center: true});   
        $("#logoContainer").fullLogoImg("/imgs/splash_logo.png", {fadeInSpeed: 2000, center: true});
        $("#logoContainer").click(function(){

                $("#wrapper").show("fast");
                $("#splash_kill").remove();
                $(this).remove();

        });
});
    </script>';
}
?>

Any help would be much appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

向地狱狂奔 2024-09-18 15:53:28

该通知告诉您未设置 HTTP_REFERER。

'HTTP_REFERER'
将用户代理引向的页面地址(如果有)
当前页面。这是由用户设置的
代理人。并非所有用户代理都会设置
这,有些提供了能力
修改 HTTP_REFERER 作为一项功能。在
简而言之,它确实不能被信任。

http://php.net/manual/en/reserved.variables.server。 php

快速搜索导致了这一点,其中提供了更多详细信息: http://www.electrictoolbox.com/php-http-referer-variable/

更新:

您可以尝试使用会话变量。该变量可以是一个简单的布尔值,如果已显示初始页面,则将其设置为 true,否则设置为 false。这需要在可能是网站入口点的每个页面和/或您想要显示启动画面的每个页面上进行一些会话处理。

http://php.net/manual/en/function.session-start。 php

当然,这仅在会话期间有效。

因此,您可以使用 cookie 来代替或采用这种方法。

就我个人而言,我可能会选择简单的 cookie 方法,因为这只是一个飞溅。无需为如此简单的事情添加会话开销。

The notice is telling you that a HTTP_REFERER isn't set.

'HTTP_REFERER'
The address of the page (if any) which referred the user agent to the
current page. This is set by the user
agent. Not all user agents will set
this, and some provide the ability to
modify HTTP_REFERER as a feature. In
short, it cannot really be trusted.

http://php.net/manual/en/reserved.variables.server.php

A quick search led to this, which provides a little more detail: http://www.electrictoolbox.com/php-http-referer-variable/

Update:

You could try using a session variable. The variable could be a simple boolean, which is set to true if the splash page has been displayed, false otherwise. This would require some session handling on every page that may be an entry point to the website and/or every page where you'd want to display the splash.

http://php.net/manual/en/function.session-start.php

Of course, this would only be viable for the duration of the session.

So, you could use a cookie instead or with this approach.

Personally, I would probably opt for the simple cookie approach, as this is just a splash. No need to add the overhead of sessions for something this simple.

梦里人 2024-09-18 15:53:28

这就是我正在尝试的。我不确定通配符:

 <?php
 //echo $ref
 $ref = $_SERVER["HTTP_REFERER"];
 //if is NOT coming from inside linking
 if ( $ref != '*www.example*' )
 // send to index and exit
 {header("Location:http://www.example.com/index.php");
 Exit;
 //
 }
 ?>

This is what I am trying. I am not sure about the wildcard:

 <?php
 //echo $ref
 $ref = $_SERVER["HTTP_REFERER"];
 //if is NOT coming from inside linking
 if ( $ref != '*www.example*' )
 // send to index and exit
 {header("Location:http://www.example.com/index.php");
 Exit;
 //
 }
 ?>
何必那么矫情 2024-09-18 15:53:28

虽然您不能完全依赖 $_SERVER['HTTP_REFERER'] 进行设置,但我认为您会发现它对于启动页面来说足够可靠。如果未设置,则没有引用者,并且该引用者将为空白,因为它不在标头中。

您应该首先测试是否为空($_SERVER['HTTP_REFERER']),然后检查其中是否有您自己的域。

另一方面,我正在使用一台旧的 IIS 服务器,该服务器更改了其中一些 $_SERVER 变量的名称,并且通常会弄乱它们。我会执行 print_r($_SERVER) 来查看发生了什么。

While you can't completely rely on $_SERVER['HTTP_REFERER'] to be set, I think you will find that it is reliable enough for a splash page. When it isn't set, there is no referer and that will be blank, as it isn't in the header.

You should first test to see if empty($_SERVER['HTTP_REFERER']) and then check to see if it has your own domain in it.

On another note, I was working on an old IIS server that changed the names of some of those $_SERVER variables, and generally messed them up. I'd do a print_r($_SERVER) to see what's up.

白况 2024-09-18 15:53:28
if(isset($_SERVER['HTTP_REFERER'])&&!empty($_SERVER['HTTP_REFERER'])) {
    $http_referer=$_SERVER['HTTP_REFERER'];
}
if(isset($_SERVER['HTTP_REFERER'])&&!empty($_SERVER['HTTP_REFERER'])) {
    $http_referer=$_SERVER['HTTP_REFERER'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文