通过电子邮件报告错误 404,最后访问 URL
report error 404 by email with last accessed URL
我正在制作一个带有按钮的 404 页面,用于用户报告网站有问题。这个想法是当用户点击按钮时,我会收到一封电子邮件,其中包含访问的最后一页的信息...
问题是我正在使用 php 变量“$_SERVER['HTTP_REFERER']”,而这个 return 是一个空值。我发现了这个问题 (),我得出的结论是这个变量不是解决方案。
这是我的 .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 ():
第 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= [R=301,L]
根据您的具体设置,您可能需要将最后一行调整为
Rewriterule ^(.*) http://example.com/erro.php?error_path= [R=301,L]
我发现 mod_rewrite
有点类似于巫术,有时需要一点点 "try it out and see what works"。
然后在您的 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
作弊sheet:http://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/
mod_rewrite
上的官方文档:http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
我正在制作一个带有按钮的 404 页面,用于用户报告网站有问题。这个想法是当用户点击按钮时,我会收到一封电子邮件,其中包含访问的最后一页的信息...
问题是我正在使用 php 变量“$_SERVER['HTTP_REFERER']”,而这个 return 是一个空值。我发现了这个问题 (),我得出的结论是这个变量不是解决方案。
这是我的 .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 ():
第 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= [R=301,L]
根据您的具体设置,您可能需要将最后一行调整为
Rewriterule ^(.*) http://example.com/erro.php?error_path= [R=301,L]
我发现 mod_rewrite
有点类似于巫术,有时需要一点点 "try it out and see what works"。
然后在您的 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
作弊sheet:http://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/mod_rewrite
上的官方文档:http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html