Forum Moderators: open

Message Too Old, No Replies

How to Implement HTTP REFERER in my subscription form

Implementing HTTP_REFERER for subscription forms

         

killua

5:35 am on Feb 5, 2021 (gmt 0)

10+ Year Member



I have a static .html site and I want to implement a very simple subscription form. It will be implemented together with an external javascript lightbox popup hosted by a third-party developer which is why my subscription form will only be a simple one.

I don't have much knowledge in HTML/PHP programming. So upon searching online, I found a template code that looks like below:

In my .html page:

<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<input type="submit" value="Send">
</form>


And in my mail.php:

<?php $name = $_POST['name'];
$email = $_POST['email'];
$formcontent="From: $name";
$recipient = "email@sample.com";
$subject = "Subscription Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>


So far, the above is working. BUT I also want to automatically include the referring page on the form, that is the page within my website where they submitted the info in the subscription form. Because the subscription form will be on many different pages of the site, obtaining this information is valuable so I know what page is generating the most subscription.

I understand that there's a feature called "HTTP_REFERER"

But I don't know how to code and where to put that code so that the referral link will also be submitted to me on the email.

Can someone help with what I need to do or add using the sample codes I used above?

Note that I understand security issues but this is only a basic sample form (security modifications will be added later on) and I just need to know how HTTP_REFERER can be added.

Many thanks!

JorgeV

8:43 am on Feb 5, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Hello,

In your PHP code:

$referer = $_SERVER['HTTP_REFERER'];

Now, keep in mind that the HTTP_REFERER field is set by the web browser. Some browsers, or extensions can hide it, or send something else.

killua

10:27 am on Feb 5, 2021 (gmt 0)

10+ Year Member



Thanks. I now have added that line so my mail.php script now looks like below:

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name";
$recipient = "email@sample.com";
$subject = "Subscription Form";
$mailheader = "From: $email \r\n";
$referer = $_SERVER['HTTP_REFERER'];
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>


Now what should I add in my .html <form> code in my original post so that the HTTP_REFERER info will be sent?

JorgeV

11:05 am on Feb 5, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Now what should I add in my .html <form> code in my original post so that the HTTP_REFERER info will be sent?

Nothing.

killua

11:16 am on Feb 5, 2021 (gmt 0)

10+ Year Member



I have tested it but the email it will send only shows the Name and Email of the visitor. Tested in Chrome and Firefox. Maybe some additional codes are lacking?

not2easy

12:58 pm on Feb 5, 2021 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



As JorgeV explained, there is no code to force the browser to send that information. Most browsers do not send referer information. If you view your access logs you may see referer information but most of that today is a field used by scripts/bots, not actual referrer data.

JorgeV

1:28 pm on Feb 5, 2021 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



I suspect you are not adding the $referer to the body of the message. I think you want to do something like that :

$referer = $_SERVER['HTTP_REFERER'];
$message = $_POST['message'];
$formcontent="From: $name\n\n$message\n\nReferer: $referer";

In your form, you can also populate an hidden field, with the current page URL, using Javascript (supposing JS is enabled).

ps: you mentioned it already, but we never repeat it enough, don't forget to check all the data you are receiving from the form.

killua

1:50 pm on Feb 5, 2021 (gmt 0)

10+ Year Member



Thanks. This line
$formcontent="From: $name\n\n$message\n\nReferer: $referer";
worked well!

Also, apparently, I just learned that the $referer line should be placed just before the $formcontent line, just like what you did. Otherwise it doesn't work. I didn't realize that these needs to be placed in specific line positions as well.