简体   繁体   English

使用post以表格形式传递当前URL php

[英]Pass current URL php in form using post

I have php file with html coding inside. 我有带有html编码的php文件。 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. 我正在使用include语句将同一表单导入许多不同的页面,但是我需要知道表单是从哪个页面提交的。 Files themselves are .php, however the most of coding is in html. 文件本身是.php,但是大多数编码都使用html。 How can I add the current URL of the website the form was submitted from? 如何添加提交表单的网站的当前URL? 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部分:

<?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. 我相信我需要获取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 看看下面的答案和代码

Get the full URL in PHP 获取PHP中的完整URL

<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, 传递链接要少一点, $_SERVER[HTTP_REFERRER]通常可以使用,

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. HTTP_REFERRER-是发出请求的地址,因此,在您的情况下,它应该是带有表单的页面。

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! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM