以联系表格生成唯一代码/ ID? PHP / AJAX

时间:2016-11-02 11:19:21

标签: php contact-form ajaxform

您好我有我的联系表格,一切正常我现在要做的是让PHP生成一个唯一的代码/ ID,例如“002302103”,并在提交电子邮件时将其附加到电子邮件中。 基本上是每次使用表单时生成的随机引用号,并且该随机数字集也随电子邮件一起发送。 有谁知道我怎么回事?

这是我的php代码:

    <?php
if($_POST)
{
    $to_Email       = "MyEmail@Email.com"; 
    $subject        = 'Message from my website'; //Subject line for emails


    //check if its an ajax request, exit if not
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

        //exit script outputting json data
        $output = json_encode(
        array(
            'type'=>'error', 
            'text' => 'Request must come from Ajax'
        ));

        die($output);
    } 

    //check $_POST vars are set, exit if any missing
    if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userMessage"]))
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
        die($output);
    }

    //Sanitize input data using PHP filter_var().
    $user_Name        = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
    $user_Email       = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
    $user_Message     = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);

    $user_Message = str_replace("\&#39;", "'", $user_Message);
    $user_Message = str_replace("&#39;", "'", $user_Message);

    //additional php validation
    if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
        die($output);
    }
    if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
        die($output);
    }
    if(strlen($user_Message)<5) //check emtpy message
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
        die($output);
    }

    //proceed with PHP email.
    $headers = 'From: '.$user_Email.'' . "\r\n" .
    'Reply-To: '.$user_Email.'' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    $sentMail = @mail($to_Email, $subject, $user_Message . "\r\n\n"  .'-- '.$user_Name. "\r\n" .'-- '.$user_Email, $headers);

    if(!$sentMail)
    {
        $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
        die($output);
    }else{
        $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_Name .'! Thank you for your email'));
        die($output);
    }
}
?>

提前感谢您帮助/指出我正确的方向

2 个答案:

答案 0 :(得分:1)

如上所述使用time()将为您提供一种创建唯一ID的可排序方式。连接字符串还会进一步随机化您想要的结果,并仍然可以对其进行排序:

$uniqueId= time().mt_rand();

答案 1 :(得分:1)

尝试这样的事情:

echo mt_rand() . "\n";
echo mt_rand() . "\n";

它的输出是:

1604716014
1478613278

mt_rand() Reference:

将此附加到您的邮件中:

$subject = 'Message from my website - '.$mt_rand();