我正在制作一个带有按钮的 404 页面,供用户报告该网站有问题。这个想法是当用户单击按钮时,我会收到一封电子邮件,其中包含最后访问的页面的信息...
问题是我正在使用 php 变量 '$_SERVER['HTTP_REFERER']' 并且它返回一个空值。我发现了这个问题( In what cases will HTTP_REFERER be empty ),我得出的结论是这个变量不是解决方案。
这是我的.htacess
RewriteEngine on
ErrorDocument 404 http://example.com/erro.php
#ErrorDocument 404 /erro.php #this doesnt work... redirect doesnt work
在 erro.php 页面上,我有带有电子邮件功能的代码,可以正常工作,但我需要某种方式来获取上次访问的生成错误的页面。
在 erro.php 页面上我尝试使用:
echo $_SERVER['HTTP_REFERER']; // return null value
echo $_SERVER['REQUEST_URI']; // return page http://example.com/erro.php
我尝试使用 jQuery 的替代方案 ( https://stackoverflow.com/a/2415645/2761794 ):
在 erro.php 页面上
$(document).ready(function() {
var referrer = document.referrer;
alert(referrer); // return null value
});
关于使用 erro.php 页面上上次访问的 URL 通过电子邮件发送的建议?
最佳答案
由于您无论如何都愿意使用 mod_rewrite
并拥有 PHP
,因此有一种稍微不同的方法。
首先,您可以检测针对不存在的文件或目录的请求,然后通过重写将其传递给 PHP
脚本。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^(.*) /erro.php?error_path=$1 [R=301,L]
根据您的具体设置,您可能需要调整最后一行
Rewriterule ^(.*) http://example.com/erro.php?error_path=$1 [R=301,L]
我发现 mod_rewrite
有点类似于巫毒,有时需要一点“尝试一下,看看有什么作用”。
然后在您的 erro.php
文件中:
<?php
$badfile = $_GET['error_path']; // the URL that 404'd
http_response_code(404); // send the 404 header code
// ... your other code
访问者的最终结果应该几乎相同,但您可以访问所需的数据。
- 大部分重写指令都来自这个问题:htaccess errordocument 404 and pass url to path
- 有关在 php 中设置响应代码的更多信息:http://php.net/manual/en/function.http-response-code.php
mod_rewrite
备忘单:http://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/mod_rewrite
的官方文档:http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
关于php - 通过包含上次访问 URL 的电子邮件报告错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29987309/