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.
Take a look at the answer and code below
<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>
<?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>
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.