feedback form keywords blacklist

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
marchello
New php-forum User
New php-forum User
Posts: 4
Joined: Mon Oct 30, 2017 6:10 am

Tue Jun 26, 2018 10:06 am

Hi all,

I'd like to have keywords black list for my feedback form. Right now it works fine with just one keyword1 (see below). What to do with more keyword2, keyword3 and also in upper case? As I have almost no php experience, I would do it using several if-then-else blocks, though someone might kindly suggest me more elegant solution.
Please advise.
Thanks ahead.

Code: Select all

<?php

function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}

        $user_ip = getUserIP();
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        $ref = @$_SERVER[HTTP_REFERER];
        $nl = nl2br("   |||||   ");
        $email_domain = substr($_REQUEST['email'], 0, strrpos($_REQUEST['email'], '@'));

        $headerFields = array(
            "MIME-Version: 1.0",
            "Content-Type: text/html;charset=utf-8"
        );

        if (!filter_var($_REQUEST['email'], FILTER_VALIDATE_EMAIL)) {
            $desc = $desc.'Email is not valid.';
            $host  = $_SERVER['HTTP_HOST'];
            $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
            $extra = 'error.php';
            header("HTTP/1.1 409 CONFLICT");
            header("Location: http://$host$uri/$extra");
        } else
        if (strpos($_REQUEST['message'], 'keyword1') !== false) {
            $desc = $desc.'Forbidden content.';
            $host  = $_SERVER['HTTP_HOST'];
            $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
            $extra = 'error3.php';
            header("HTTP/1.1 409 CONFLICT");
            header("Location: http://$host$uri/$extra");
        } else ... 

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 743
Joined: Mon Feb 22, 2016 5:52 pm

Tue Jun 26, 2018 1:47 pm

Put your keywords in an array and use a built in array search function, I'm sure you can work out how to do a case insensitive search.

Post Reply