gpt4 book ai didi

php - 这种形式安全吗?

转载 作者:太空宇宙 更新时间:2023-11-04 13:36:42 24 4
gpt4 key购买 nike

我有这个表单,用户可以通过该表单向我发送电子邮件。我不知道它是否安全,或者是否仅在涉及 sql 时才会出现安全问题...

html:

<form id="form4" action="send_mic.php"  name="form4" method="post" >

<textarea name="message4" cols="4" rows="4" id="message4" ></textarea><br />

<input type="text" id="name4" name="name4" value="" /><br />

<input type="text" id="email4" name="email4" value="" /><br />

<input type="submit" value="" id="submit" />

</form>

j查询:

<script type="text/javascript">
$(document).ready(function () {
$('#form4').ajaxForm({
beforeSubmit: validate
});

function validate(formData, jqForm, options) {
var name = $('input[name=name4]').fieldValue();
var email = $('input[name=email4]').fieldValue();
var message = $('textarea[name=message4]').fieldValue();

if (!name[0]) {
alert('Please enter a value for name');
return false;
}
if (!email[0]) {
alert('Please enter a value for email');
return false;
}
if (!message[0]) {
alert('Please enter a value for message');
return false;
}

else {

$("#content").fadeOut(1000, function () {
$(this).html("<img src='images/postauto3.png'/>").fadeIn(2000);
});

var message = $('textarea[name=message4]').val('');
var name = $('input[name=name4]').val('');
var email = $('input[name=email4]').val('');

}
}

});



</script>

PHP:

<?php
if($_POST){
$email = $_POST['email4'];
$name = $_POST ['name4'];
$message = $_POST ['message4'];
// response hash
$ajaxresponse = array('type'=>'', 'message4'=>'');

try {
// do some sort of data validations, very simple example below
$all_fields = array('name4', 'email4', 'message4');

foreach($all_fields as $field){
if(empty($_POST[$field])){
throw new Exception('Required field "'.ucfirst($field).'" missing input.');
}
}

// ok, if field validations are ok
// now Send Email, ect.

// let's assume everything is ok, setup successful response
$subject = "New Contact";
//get todays date
$todayis = date("l, F j, Y, g:i a") ;

$message = " $todayis \n
Attention: \n\n
Please see the message below: \n\n
Email Address: $email \n\n
Message: $message \n\n

";

$from = "From: $email\r\n";


//put your email address here
mail("contact@....ro", $subject, $message, $from);

//prep json response
$ajaxresponse['type'] = 'success';
$ajaxresponse['message'] = 'Thank You! Will be in touch soon';
} catch(Exception $e){
$ajaxresponse['type'] = 'error';
$ajaxresponse['message'] = $e->getMessage();
}
// now we are ready to turn this hash into JSON
print json_encode($ajaxresponse);
exit;
}
?>

那么,使用表单发送邮件会不会存在安全问题呢?这个可以吗?谢谢!

最佳答案

一般而言,经验法则应始终是:永远不要相信用户提供的数据。不,您的代码不是防弹的。由于您既不验证也不清理用户输入,并且同时使用 mail(),因此您很容易受到攻击。用户可以轻松地为您提供 email4 字段的精心制作的值(value)。由于您直接使用表单数据,因此 email4 可用于向您的外发邮件中注入(inject)额外的邮件 header 。如果这些 header 是 BCC:CC: 甚至 TO: 那么您将只是充当垃圾邮件中继。例如,如果我发布这个

some@address.com
CC: spamvictim1@foo.com, spamvictim2@foo.com, spamvictim3@foo.com,
X-Spam-Owned: Whoa

作为您的 email4 那么您的 header 将如下所示:

To: some@address.com
CC: spamvictim1@foo.com, spamvictim2@foo.com, spamvictim3@foo.com,
X-Spam-Owned: Whoa

要发布多行数据,您只需使用 CRLF 粘贴文本即可。

为了避免这样的安全漏洞,你应该考虑放弃 mail() 并使用更聪明的东西来处理这样的事情(不是 mail()不好,但是您需要知道自己在做什么,因为它比高级功能低)。我建议使用 PHPMailer 或类似的包。您应该始终验证用户提供的数据(特别是确保单行字段,如主题实际上是单行 - 剥离 CRLF 就足够了)。在您接受自动提交表单时添加验证码。

关于php - 这种形式安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12315112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com