• March 13, 2023

    create php form sent to mail that have criteria for check and protect spam:
    1.check ThaiCharacters must have in detail input
    2.Check input speed From the start, if less than 15 seconds, then press submit must to ban for 1 hour.
    3.user can submit the form must spent less than 50 seconds on the page
    4.prevent user from submitting again within the last 50 minutes

            $to = "mail@mail.com";

    To create a PHP form that is sent to mail and has criteria to check and protect spam, you can use the following code:

    if(isset($_POST['submit'])) {
        // check Thai characters in detail input
        if(!preg_match("/^[ก-๙เ ]+$/u", $_POST['detail'])) {
            echo "Please enter Thai characters in the detail input field only.";
        // check input speed
        if(isset($_SESSION['submit_time']) && time() - $_SESSION['submit_time'] < 15) {
            echo "You are submitting the form too quickly. Please wait for 15 seconds before trying again.";
        // check submission time spent on the page
        if(isset($_SESSION['page_load_time']) && time() - $_SESSION['page_load_time'] > 50) {
            echo "You have spent too much time on the page. Please refresh the page and try again.";
        // prevent user from submitting again within the last 50 minutes
        if(isset($_SESSION['last_submit_time']) && time() - $_SESSION['last_submit_time'] < 3000) {
            echo "You have already submitted the form. Please wait for 50 minutes before trying again.";
        // send email
        $name = $_POST['name'];
        $topic = $_POST['topic'];
        $detail = $_POST['detail'];
        $tel = $_POST['tel'];
        $email = $_POST['email'];
        $to = "mail@mail.com";
        $subject = "New form submission";
        $message = "Name: " . $name . "\n" . "Topic: " . $topic . "\n" . "Detail: " . $detail . "\n" . "Tel: " . $tel . "\n" . "Email: " . $email;
        $headers = "From: " . $email;
        mail($to, $subject, $message, $headers);
        // set session variables
        $_SESSION['submit_time'] = time();
        $_SESSION['page_load_time'] = time();
        $_SESSION['last_submit_time'] = time();
        // redirect to thank you page
        header("Location: thank_you.php");

    Explanation of the code:

    • The code starts by starting a PHP session using session_start().
    • When the form is submitted, the code first checks if Thai characters are entered in the detail input field. This is done using a regular expression that only allows Thai characters and spaces. If the input is not in Thai characters, the code displays an error message and exits.
    • The code then checks the input speed. If the user is submitting the form within 15 seconds of the previous submission, the code displays an error message and exits.
    • The code also checks the time spent on the page. If the user has spent more than 50 seconds on the page, the code displays an error message and exits.
    • The code also prevents the user from submitting the form again within the last 50 minutes. If the user has already submitted the form within the last 50 minutes, the code displays an error message and exits.
    • If all the checks pass, the code sends an email with the form data to the specified email address.
    • The code then sets session variables to track the submission time, page load time, and last submission time.
    • Finally, the code redirects the user to a thank you page.

    Note: The code assumes that you have a thank you page (thank_you.php) where the user is


ประหยัดเวลากว่า 100 เท่า!

สร้างเว็บไซต์ ดูแลเว็บไซต์