dongruolin5324 2015-10-27 09:46
浏览 64
已采纳

如何保护发送电子邮件的简单PHP脚本?

In a simple contact form, the HTML form triggers a Php script :

<form method="post" action="email.php">.....</form>

This email.php sends me an email :

<?php
    $mess=$_POST['name']. "
" .$_POST['site']. "
" .$_POST['email']. "
" .$_POST['meta']. "
" .$_POST['message'];
    mail('myemail@gmail.com', 'Subject', $mess);
?>

The HTML is using jQuery validate plugin, but nothing like this on the Php side.

A Security expert told me how insanely unsecure this php script was.

What can I do to enhance security ?

  • 写回答

3条回答 默认 最新

  • dqunzip3183 2015-10-27 14:47
    关注

    My guess is that your "security expert" saw you using raw $_POST data and using the mail() function, and he freaked out, but didn't stop to actually check how bad things were.

    He has a point in that using $_POST without doing any validation on it is almost always a recipe for being hacked, but in fact in this particular case I don't think it's too bad, because you are the only recipient (so it's not going to be used for spam, which is the main thing to worry about in these cases), and because the body is plain text (so a hacker can't send you any nasty scripts or attachments).

    Without any validation, you could get some really weird emails as a result of hackers trying to find a way around your defences, but not too much else.

    PHP's mail() function is a well-known soft target for hackers because there is an awful lot of insecure code out there that uses it. However the real danger with mail() tends to be if you use the headers parameter (ie to set things like the sender address, etc), which you haven't used. Since you're not using headers, the risks are a lot lower, and mainly limited to making it easy for someone to mailbomb you.

    If you are still worried about the security of the mail() function, the best solution is to use a library like phpMailer instead.

    To be honest, my advice whenever anyone wants to use PHP's mail() function is always to use phpMailer or Swiftmailer instead. And it's not even just about security; even for simple cases, they can make your code a lot easier to read and maintain.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备