为了账号安全,请及时绑定邮箱和手机立即绑定

POST请求空白,为什么?

POST请求空白,为什么?

PHP
森栏 2021-05-13 14:12:25
我将以下代码另存为send.php:<?php$to_email = "person@gmail.com";$subject = $_POST['subject'];$body = "Hi, This is test email send by PHP Script";$headers = "From: Me@myWebsite.com";if ( mail($to_email, $subject, $body, $headers)) {    echo("Email successfully sent to $to_email...");} else {    echo("Email sending failed...");}?>如果我导航到https://myWebsite.com/Stuff/send.php 并发送电子邮件,该电子邮件将发送$subject = "something"但是如果我设置$subject为POST变量。当我在浏览器中写入此URL时,它不会发送主题(例如,电子邮件仍然发送,但主题现在为空): https://myWebsite.com/Stuff/send.php?subject=notworking
查看完整描述

3 回答

?
梦里花落0921

TA贡献1772条经验 获得超6个赞

要获得两者$_POST ,$_GET您可以使用$_REQUEST适用于两者的东西。


在您的代码中替换


$subject = $_GET['subject'];


$subject = $_REQUEST['subject'];

如果有人直接在浏览器中直接点击了网址,但没有设置主题,则可以阻止他们使用


if(!empty($_REQUEST['subject']))

{

  $to_email = "person@gmail.com";

  $subject = $_REQUEST['subject'];

  $body = "Hi, This is test email send by PHP Script";

  $headers = "From: Me@myWebsite.com";


  if ( mail($to_email, $subject, $body, $headers))

    echo("Email successfully sent to $to_email...");

  else

    echo("Email sending failed...");

}


查看完整回答
反对 回复 2021-05-28
?
杨__羊羊

TA贡献1943条经验 获得超7个赞

尝试一下。


 <?php

    $to_email = "person@gmail.com";

    $subject = (isset($_GET['subject']) && (!empty($_GET['subject']))?$_GET['subject']:"No Subject";

    $body = "Hi, This is test email send by PHP Script";

    $headers = "From: Me@myWebsite.com";


    if ( mail($to_email, $subject, $body, $headers))

        echo("Email successfully sent to $to_email...");

    else

        echo("Email sending failed...");

?>


查看完整回答
反对 回复 2021-05-28
?
子衿沉夜

TA贡献1828条经验 获得超3个赞

如果您要从网址参数中提取值,则需要使用$_GET 全局变量来获取这样的数据,


<?php

$to_email = "person@gmail.com";

$subject = $_GET['subject'];

$body = "Hi, This is test email send by PHP Script";

$headers = "From: Me@myWebsite.com";


if ( mail($to_email, $subject, $body, $headers)) {

    echo("Email successfully sent to $to_email...");

} else {

    echo("Email sending failed...");

}

?>


查看完整回答
反对 回复 2021-05-28
  • 3 回答
  • 0 关注
  • 301 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信