0

I have php file with html coding inside. I'm using include statement to import the same form into many different pages, however I need to know which page the form was submitted from. Files themselves are .php, however the most of coding is in html. How can I add the current URL of the website the form was submitted from? I use post method.

<form action="post.php" method="post">
   <input type="hidden" name="url" value="(Current URL here)" />
   <input type="text" id="email" name="email">
</form>

and php part:

<?php
  $addressto = "mail@mail.com";
  $subject = "Message";
  $content = "Email: ".$_POST['email']."\n"
            ."URL: ".$_POST['url']."\n";

    $email = $_POST['email'];
    if (mail($addressto, $subject, $content, 'From: <' . $email . '>')) {
        header("Location: message-sent.html");
    }
?>

I believe I need some sort of code that gets URL. I found few similar questions here but none of them clearly explains how to do it. Thank you for your help.

Piotr Ciszewski
  • 1,691
  • 4
  • 30
  • 53

3 Answers3

2

<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

?>

<form action="post.php" method="post">
   <input type="hidden" name="url" value="<?=$actual_link?>" />
   <input type="text" id="email" name="email">
</form>
1

Take a look at the answer and code below

Get the full URL in PHP

<form action="post.php" method="post">
  <input type="hidden" name="url" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>" />
  <input type="text" id="email" name="email">
</form>
Jack Templeman
  • 344
  • 1
  • 9
0

Passing a link is a bit point less $_SERVER[HTTP_REFERRER] will generally work,

It's subject to client modification but so are form fields. I would check the domain on the back end just to be safe if you really want to.

HTTP_REFERRER - is the address making the request, so in your case it should be the page with the form.

One less variable to handle.

  $content = "Email: ".$_POST['email']."\n"
        ."URL: ".$_SERVER[HTTP_REFERRER]."\n";


 $email = $_POST['email'];
 if (mail($addressto, $subject, $content, 'From: <' . $email . '>')) {
    header("Location: message-sent.html");
 }

Cheers!

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38