在阅读了大量教程之后,我只是想确保安全。
我做了一个像这样的接触公式
<form name="contakt" accept-charset="UTF-8" method="post" action="./mail.php">
<input type="text" name="name" />
<input type="text" name="email" />
<input type="text" name="tel" />
<input type="submit" value="Send" name="submit" />
<textarea name="message"></textarea>
</form>
我通过jQuery验证如果名称和消息不是空的,不是只有空格
和我检查电子邮件通过jquery与以下脚本
function ismailornot(email) {
var regex = /^([a-zA-Z0-9_.+-])+'@(([a-zA-Z0-9-])+'.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
现在,当我的变量被传递,我在我的邮件。php是足够的顶部检查我的脚本的$_SERVER['HTTP_REFERER'],看看这些变量是否来自我自己的脚本?或者你也可以修改$_SERVER变量吗?
或者我是否需要再次检查每个传递的变量以确保安全?
例如:http://www.w3schools.com/php/php_secure_mail.asp这个脚本100%安全吗?
谢谢你的帮助:)
方法:为安全起见,再次检查每个传递的变量
在一些mod之后试试这个,以满足您的需求这是来自Larry Ullman的一本书:
function spam_scrubber($value) {
// List of very bad values:
$very_bad = array('to:', 'cc:', 'bcc:', 'content-type:', 'mime-version:','multipart-mixed:',
'content-transfer-encoding:', '<script>');
// If any of the very bad strings are in
// the submitted value, return an empty string:
foreach ($very_bad as $v) {
if (stripos($value, $v) !== false){ return '';}
}
// Replace any newline characters with spaces:
$value = str_replace(array( "'r", "'n", "%0a", "%0d"), ' ', $value);
//remove html tags:
$value = htmlentities($value,ENT_QUOTES);
// Return the value:
return trim($value);
} // End of spam_scrubber() function.
// Clean the form data:
$scrubbed = array_map('spam_scrubber', $_POST);
if(isset($from)) {
$from = $scrubbed['from'];
}else{
$from = '';
}
// Minimal form validation:
if (!empty($from) && !empty($scrubbed['comments']) ) {
// Create the body:
$body = "Name: {$from}'n'nComments: {$scrubbed['comments']}";
$body = wordwrap($body, 70);
// Send the email:
mail('YOUR_EMAIL', 'Contact Form Submission', $body, "From: {$from}");
}