简体   繁体   English

PHP HTTP_REFERRER-如何检测最后一页?

[英]PHP HTTP_REFERRER - how to detect last page?

I need to detect where the user has just clicked from - as my AJAX content needs to be displayed differently depending on the source page it is to be inserted into. 我需要检测用户刚刚单击的位置-因为我的AJAX内容需要根据要插入的源页面的不同显示。

If it's to go into about.php it needs to be data only, but if it's to go into about-main.php it needs to be the whole middle column so needs a header/footer wrapper around the data. 如果要进入about.php,则只需要是数据,但如果要进入about-main.php,则必须是整个中间列,因此需要在数据周围放置页眉/页脚。

The html called via AJAX is held in a php page which uses this code to see who's asking, and then formats the HTML response appropriately. 通过AJAX调用的html保留在php页面中,该页面使用此代码查看谁在询问,然后适当地格式化HTML响应。

$array[] = "/cf/about.php";
$array[] = "/cf/about/about-main.php";
$array[] = "/cf/about/other-page.php";


$ispage = "";
foreach ($array as $value) {
    if ($_SERVER['HTTP_REFERER'] != $value) {
        $ispage = "";
    } else {
        $ispage = "woot";
    }
}
if ($ispage == "woot") {
        echo $content;
    } else {
        include_once 'about-header.php';
        echo $content;  
        include_once 'about-footer.php';
}

The problem is... HTTP_REFERER seems to be a bit hit and miss. 问题是... HTTP_REFERER似乎有点麻烦。 It works just fine while I'm at work on the network, but I've tried it out on my computer at home and it's obviously completely failing to work - the results are horrible :o 当我在网络上工作时,它可以正常工作,但是我已经在家里的计算机上试用了它,显然完全无法正常工作-结果太糟糕了:o

Is there another way of achieving this? 还有另一种方法可以实现这一目标吗? I guess session variables could be used but then I've not got much experience of that! 我想可以使用会话变量,但是我对此没有太多经验!

Any and all hints/tips are appreciated ;) Thanks! 任何和所有的提示/技巧表示赞赏;)谢谢!

edit: 编辑:

The page is actually a staff profile page. 该页面实际上是员工资料页面。 Its normal location is about.php and the 2nd column div displays a grid of thumbnails which when clicked, load the profile in that place via AJAX. 它的正常位置是about.php,第二列div显示一个缩略图网格,当单击该缩略图时,可通过AJAX在该位置加载配置文件。 All nice and simple - back button reloads the grid of photos. 一切都很简单-后退按钮可重新加载照片网格。

The problem is, each staff member also needs a static page. 问题是,每个工作人员还需要一个静态页面。 I've created these at about/staff-name.php. 我已经在about / staff-name.php中创建了这些文件。 The content is THE SAME though. 内容虽然相同。 I want the server to detect if someone has come to the about/staff-name.php directly and if so, wrap a header/footer around it. 我希望服务器检测是否有人直接访问about / staff-name.php,如果是,则在其周围包裹一个页眉/页脚。

If the request has come from the grid of photos (ie AJAX) then it doesn't need the header/footer wrapper. 如果请求来自照片网格(即AJAX),则不需要页眉/页脚包装。

Is that clear? 明白了吗? :o :o

1) If AJAX request - no wrapper 2) If not AJAX request - add header/footer wrapper 1)如果AJAX请求-没有包装器2)如果不是AJAX请求-添加页眉/页脚包装器

Wouldn't it be easier to just pass a flag in your AJAX call to tell the script which type of content to display? 仅在AJAX调用中传递一个标志来告诉脚本显示哪种类型的内容会更容易?

Edit: 编辑:

So about/staff-name.php displays the content. 因此about / staff-name.php将显示内容。 Call it via AJAX as about/staff-name.php?FromAjax=1 通过AJAX调用about / staff-name.php?FromAjax = 1

Then in the about/staff-name.php file: 然后在about / staff-name.php文件中:

if (isset($_REQUEST['FromAjax']) ) {
    echo $content;
} else {
    include_once 'about-header.php';
    echo $content;  
    include_once 'about-footer.php';
}

No matter what, the Referer is not an information you should base your whole website upon : it is sent by the client, which means (at least) : 无论如何,推荐人不是您应该基于整个网站建立的信息:它是由客户发送的,这意味着(至少):

  • the client does not necessarily have to send it 客户不一定必须发送它
    • it can be disabled in the browser 可以在浏览器中禁用它
    • some firewall / antivirus remove it 一些防火墙/杀毒软件将其删除
  • it can be forged / altered (an easy way with firefox is to use an extension to do that) 可以伪造/更改它(使用Firefox的一种简单方法是使用扩展名来做到这一点)

You definitly must find a better/saffer/more reliable way to do what you need. 您绝对必须找到一种更好/更安全/更可靠的方式来完成所需的工作。

One solution (which you already discarded) would be to pass an additionnal information in all your links, saying which page the request comes from. 一种解决方案(您已经舍弃了该解决方案是在所有链接中传递附加信息,说明请求来自哪个页面。 In my opinion, this would probably be the best thing to do... 我认为这可能是最好的选择。

Maybe a simpler way would be to add a parameter to your Ajax request, saying where it comes from ? 也许更简单的方法是将参数添加到您的Ajax请求中,说明它来自何处? ie, instead of relying on the Referer in the PHP script, just have a look at a parameter in the request, which would act as some "kind of referer", but put by you ? 即,不依赖于PHP脚本中的Referer,而只是查看请求中的参数,该参数将充当某种“参照”,但是由您来放置?

It will not be more secure (users could still forge request, with that parameter) , but, at least, it would not be disabled / modified (except if the user does it by hand) 它不会更加安全(用户仍然可以使用该参数伪造请求) ,但至少不会被禁用/修改(除非用户手动执行)


In the end, you also say this : 最后,您还要这样说:

1) If AJAX request - no wrapper 1)如果AJAX请求-无包装
2) If not AJAX request - add header/footer wrapper 2)如果不是AJAX请求-添加页眉/页脚包装器

Well, if it's only a matter of determining if the PHP script was called through an Ajax request, here too, two solutions : 好吧,如果只是确定是否通过Ajax请求调用了PHP脚本的问题,这里也提供了两种解决方案:

  • Add a parameter to the Request when it's done through Ajax (you only add this parameter in the JS script doing the request ; and when the parameter is here, PHP knows it's an Ajax request) 通过Ajax完成后,向请求中添加参数(您只需在执行请求的JS脚本中添加此参数;当参数在此处时,PHP知道它是Ajax请求)
  • Or, in the PHP script, look for an X-Requested-With HTTP header, which is often here with the value XMLHttpRequest when a request is made through an Ajax call. 或者,在PHP脚本中,查找X-Requested-With HTTP标头,当通过Ajax调用进行请求时,该标头通常带有XMLHttpRequest值。 (But you should check that it's set with your JS Framework / or maybe it depends on the browser -- not sure about that :-( ) (但是您应该检查它是否已在JS Framework中设置/或者可能取决于浏览器-不确定:-()

Why don't you just create the static pages on the server without using ajax at all, including the header and footer and the profile bit? 为什么不只在服务器上创建静态页面而不使用ajax,包括页眉和页脚以及配置文件位? If the page is static you shouldn't have any need to load content using javascript. 如果页面是静态的,则无需使用javascript加载内容。

If you do need to load it using javascript, then Vex's solution is good. 如果确实需要使用javascript加载它,那么Vex的解决方案很好。 You could pass some optional parameters in the ajax call which control how the page is rendered - that way, the included page doesn't need to know about the pages that use it, it just needs to understand the parameters that tell it how to render itself. 您可以在ajax调用中传递一些可选参数,这些参数控制页面的呈现方式-这样,所包含的页面不需要了解使用该页面的页面,它只需要了解告诉其如何呈现的参数即可。本身。 You can then reuse it more easily in future. 然后,您将来可以更轻松地重用它。

I did notice an error in your code however - this probably won't work as expected: 我确实注意到您的代码中有一个错误-这可能无法按预期工作:

$ispage = "";
foreach ($array as $value) {
    if ($_SERVER['HTTP_REFERER'] != $value) {
        $ispage = "";
    } else {
        $ispage = "woot";
    }
}

If a page name is matched, but then the next one isn't, $ispage will be set back to ''. 如果页面名称匹配,但下一个页面名称不匹配,则$ ispage将设置回。 You'd probably better off doing something like: 您最好做一些类似的事情:

$ispage = "";
foreach ($array as $value) {
    if ($_SERVER['HTTP_REFERER'] == $value) {
        $ispage = "woot";
        break;
    }
}

or 要么

$ispage = '';
if (in_array($_SERVER['HTTP_REFERER'], $array)) {
    $ispage = 'woot';
}

Tai kucing - 太极拳-

If you format your links like this: 如果您按以下格式设置链接:

<a href="http://www.this-is-a-link.html?setvariablehere=yes">link</a>

Then the php just needs to be this: 然后,PHP仅需要这样:

if (isset($_REQUEST['setvariablehere']) ) {
    do something for when variable IS set
} else {
    do something for when variable IS NOT set
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM