How can PHP code be optimized to prevent automated spam posts targeting specific keywords?
To prevent automated spam posts targeting specific keywords in PHP, you can implement a filtering mechanism that checks the content of the post for any blacklisted keywords. You can create an array of keywords to check against and then use a function to scan the post content for these keywords. If any of the keywords are found, you can reject the post or take appropriate action.
// Array of blacklisted keywords
$blacklistedKeywords = array("spam", "viagra", "casino");
// Function to check for blacklisted keywords in post content
function checkForBlacklistedKeywords($postContent, $blacklistedKeywords) {
foreach ($blacklistedKeywords as $keyword) {
if (stripos($postContent, $keyword) !== false) {
// Keyword found, take action like rejecting the post
return true;
}
}
return false;
}
// Example usage
$postContent = $_POST['content'];
if (checkForBlacklistedKeywords($postContent, $blacklistedKeywords)) {
// Reject the post or take appropriate action
echo "Post contains blacklisted keywords, please revise.";
} else {
// Post is clean, proceed with processing
echo "Post is clean and can be submitted.";
}