2

I'm making a page 404 with button for user report that site have a problem. The idea is when user click on button, I receive a email with information of the last page accessed...

The problem is that I'm using the php variable '$_SERVER['HTTP_REFERER']' and this ever return a null value. I found this question (In what cases will HTTP_REFERER be empty) and I came to the conclusion that this variable is not the solution.

This is my .htacess

RewriteEngine on
ErrorDocument 404 http://example.com/erro.php
#ErrorDocument 404 /erro.php #this doesnt work... redirect doesnt work

On page erro.php I have code with function email that are working without problem, but I need some manner to take last accessed page that generate error.

On page erro.php I'm trying use:

echo $_SERVER['HTTP_REFERER']; // return null value
echo $_SERVER['REQUEST_URI']; // return page http://example.com/erro.php

I try use alternative with jQuery (https://stackoverflow.com/a/2415645/2761794):

On page erro.php

$(document).ready(function() {
    var referrer =  document.referrer;
    alert(referrer); // return null value
});

Some suggestion to take last accessed URL on page erro.php for send by email?

Community
  • 1
  • 1
rafaelfndev
  • 679
  • 2
  • 9
  • 27

1 Answers1

1

As you are willing to work with mod_rewrite anyway and have PHP there is a slightly different approach.

First you could detect for a request that is for a file or directory that is not there and pass that to the PHP script with a rewrite.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Rewriterule ^(.*) /erro.php?error_path=$1 [R=301,L]

Depending on your exact setup you may need to tweak that last line perhaps to

Rewriterule ^(.*) http://example.com/erro.php?error_path=$1 [R=301,L]

I find that mod_rewrite is somewhat akin to voodoo and can sometimes need a little "try it out and see what works".

Then in your erro.php file:

<?php
    $badfile = $_GET['error_path']; // the URL that 404'd
    http_response_code(404); // send the 404 header code
    // ... your other code

The end result for the visitor should be almost the same but you would have access to the data you need.

Community
  • 1
  • 1