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 = "[email protected]";
  $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.

1
  • <input type="hidden" name="url" value="<?php echo $_SERVER['PHP_SELF']; ?>" /> should work Commented Mar 10, 2019 at 21:15

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.