如何将HTTP_USER_AGENT添加到我的PHP表单?

时间:2016-03-14 16:49:06

标签: php html contact-form

以下是我网站上的PHP表单代码(我没写过)。 它运作良好(我可以看到从哪个页面发送消息),但是当我收到一封电子邮件时,我也想看看用户是否从智能手机或mac发送消息,如果他正在使用safari或者firefox等。我知道$_SERVER ['HTTP_USER_AGENT'];是我需要的。 问题是我不知道如何/在哪里将其添加到现有代码中,因为我不熟悉PHP语法? 非常感谢您的帮助

<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['message']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // detect & prevent header injections
  $test = "/(content-type|bcc:|cc:|to:)/i";
  foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
      exit;
    }
  }

  //send email
 mail( "djfhdss@gmail.com", "New message from: ".$_POST['name'], $_POST['message']."\nFrom this page: ". str_replace("http://", "", $_SERVER['HTTP_REFERER']), "From:" . $_POST['email'] . "\r\n" . "BCC: ffdssdd@gmail.com" );

}
?>

和JS:

// Contact Form
$(document).ready(function() {
    $("#contactfrm").submit(function(e) {
        e.preventDefault();
        var name = $("#name").val();
        var email = $("#email").val();
        var message = $("#message").val();
        var dataString = 'name=' + name + '&email=' + email + '&message=' + message;

        function isValidEmail(emailAddress) {
            var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
            return pattern.test(emailAddress);
        };
        if (isValidEmail(email) && (message.length > 1) && (name.length > 1)) {
            $.ajax({
                type: "POST",
                url: "sendfrm.php",
                data: dataString,
                success: function() {
                    $('button[name="submit"]').hide();
                    $('.error').hide()
                    $('.success').fadeIn(1000);
                }
            });
        } else {
            $('.error').fadeIn(1000);
        }
        return false;
    });

});

1 个答案:

答案 0 :(得分:0)

将其添加到mail()函数中的正文行。最简单的,我不知道我是否应该回答,或者我是否应该将其作为offtopic关闭。但在这里你就是答案(按照箭头获取更多线索):

mail( "djfhdss@gmail.com", "New message from: ".$_POST['name'], $_POST['message']."\nFrom this page: ". str_replace("http://", "", $_SERVER['HTTP_REFERER'])."\nAnd from this user agent: ". $_SERVER['HTTP_USER_AGENT'], "From:" . $_POST['email'] . "\r\n" . "BCC: ffdssdd@gmail.com" );
---------------------------------------------------------------------------------------------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^