$_SERVER['HTTP_REFERER']

2019-09-12 12:13发布

问题:

I am making a report problem link on my website and I want it to email me the last address they were on before clicking on the report link. I know/think you use $_SERVER['HTTP_REFERER'] but I dont know how to put that in the mail code?So how would you write that here is my mail code with out it.

Mail("email@email.com", "Subject", "Message");
echo "Report Sent";

回答1:

The message should be a variable that you can put information in:

$message = "Error report: <p>Last site visited: {$_SERVER['HTTP_REFERER']}</p>....";

mail("email@email.com", "Subject", $message);

Note that the HTTP_REFERER bit is placed within {} in the string. That's to force it to extrapolate the value (I don't like string concatenation).

Also note that, as has been said above, there's no guarantee that the REFERER will have the right value, or any value at all.



回答2:

Beside everything that has been told about http referers that can be sniffed, anonymizing proxies and so on, relying on the HTTP_REFERER is not a good programming standard.

Instead, if you have, for example:

http://www.example.com/application/client.php

Where users can click on

http://www.example.com/application/report_problem.php

Just pass the "...client.php" string to the "...report_problem.php" report problem handler you will create.

It's easy to pass the "originating" page link to the "report_problem", and can be done like this:

<?php
    // pages where you will append the "problem link"

    // $this_page holds an "url encoded" version of the request uri ( e.g. /application/client.php )
    $this_page = rawurlencode ( $_SERVER["REQUEST_URI"] );
?>
<a href="report_problem.php?originating_page=<?=$this_page;?>">Report problem</a>

Then, in the "report_problem.php" code:

<?php
     // report_problem.php

     $originating_page = ( array_key_exists ( 'originating_page', $_GET ) && ! empty ( $_GET['originating_page'] ) ? rawurldecode ( $_GET['originating_page'] ) : null;

     if ( ! empty ( $originating_page ) ) {
              $message = 'Error report: <p>Last site visited: ' . $originating_page . '</p>....';
              mail("email@email.com", "Subject", $message);
     }
     else mail("email@email.com", "Subject", "Problem from unkown page");
 ?>