Hi all,

I'm trying to use the Akismet PHP5 plugin to stop spam being sent through my contact forms. The forms being on a standard PHP page and not in WordPress or anything else. I'm struggling to understand how exactly to implement it on other fields than the ones provided in the Akismet class file. Below is my code (left out API key etc on purpose), however am unsure if i'm supposed to use setCommentContent for all input fields which are not either author, email, url or comment?

Also unsure of how I can really test this, e.g. what would be classed as spam to input into the form to check? I've done stuff in procedural PHP, so by just be my lacking in OOPHP. Any advice would be appreciated.

<?php

require_once ('classes/Akismet.class.php');
$akismet = new Akismet( "http://mywebsite.com", "API KEY HERE");


// checks if key is valid
if($akismet->isKeyValid()) {
  // api key is okay
} else {
 // api key is invalid
}


if (isset($_POST['submit'])) {


// assign variable
$_POST['name'] = $name;
$_POST['department'] = $department;
$_POST['url'] = $url;
$_POST['email'] = $email;
$_POST['telephone'] = $telephone;
$_POST['comment1'] = $comment1;
$_POST['comment2'] = $comment2;


$akismet->setCommentType("enquiry");
$akismet->setCommentAuthor( $name );
$akismet->setCommentContent( $department );
$akismet->setCommentAuthorURL( $url );
$akismet->setCommentAuthorEmail( $email );
$akismet->setCommentContent( $telephone );
$akismet->setCommentContent( $comment1 );
$akismet->setCommentContent( $comment2 );


if( $akismet->isCommentSpam() )
{
	echo 'This email contains spam!';

}
else
{
	echo 'All looks okay here, send data to mailbox';
	// add send to email info here.
}

}


?>

<!DOCTYPE html>
<html dir="ltr" lang="en-GB">
<head>
	<meta charset="UTF-8" />
	<title></title>
	<meta name="viewport" content="width=device-width, minimum-scale=1.0" />
	<meta name="description" content="" />
		<link rel="stylesheet" href="css/style.css" />
		<!--[if lt IE 9]>
			<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
		<![endif]-->
</head>
<body>

<main role="main">

	<form action="" method="post">

		<ul>
			<li>
				<label for="name">Name</label>
				<input type="text" id="name" name="name" value="" />
			</li>
			<li>
				<label for="department">Department</label>
				<input type="text" id="department" name="department" value="" />
			</li>
			<li>
				<label for="url">URL</label>
				<input type="text" id="url" name="url" value="" />
			</li>
			<li>
				<label for="email">Email</label>
				<input type="text" id="email" name="email" value="" />
			</li>	
			<li>
				<label for="telephone">Telephone</label>
				<input type="text" id="telephone" name="telephone" value="" />
			</li>				
			<li>
				<label for="comment1">Comment 1</label>
				<textarea id="comment1" name="comment1" cols="20" rows="4"></textarea>
			</li>
			<li>
				<label for="comment2">Comment 2</label>
				<textarea id="comment2" name="comment2" cols="20" rows="4"></textarea>
			</li>
			<li>
				<input type="submit" name="submit" value="Submit" />
			</li>

	</form>

</main>

</body>
</html>

Thanks.

    Simplest anti spam approach is to add a hidden text field, bots tend to fill in every field, so if your hidden field is no longer empty it's spam

      Hi cretaceous,

      Thanks. The 'honey pot' method you mention is one which I may well add to this in addition to Akismet, however would really like to know how to implement the Akismet one.

      cretaceous;11031565 wrote:

      Simplest anti spam approach is to add a hidden text field, bots tend to fill in every field, so if your hidden field is no longer empty it's spam

        You need to reverse the order of the variables in this chunk:

            // assign variable
            $_POST['name'] = $name;
            $_POST['department'] = $department;
            $_POST['url'] = $url;
            $_POST['email'] = $email;
            $_POST['telephone'] = $telephone;
            $_POST['comment1'] = $comment1;
            $_POST['comment2'] = $comment2; 
        

        I.e.: you want to assign $_POST['comment2'] to $comment2, but you are doing the inverse.

          Doh! Thanks not sure how I managed to assign these the wrong way around! Anyone able to help with the rest?

          NogDog;11031571 wrote:

          You need to reverse the order of the variables in this chunk:

              // assign variable
              $_POST['name'] = $name;
              $_POST['department'] = $department;
              $_POST['url'] = $url;
              $_POST['email'] = $email;
              $_POST['telephone'] = $telephone;
              $_POST['comment1'] = $comment1;
              $_POST['comment2'] = $comment2; 
          

          I.e.: you want to assign $_POST['comment2'] to $comment2, but you are doing the inverse.

            I'm not familiar with the Akisment API, but if it makes sense to use that method for more than one textual field, I might just concatenate them together, e.g.:

            $akismet->setCommentContent( "$telephone $comment1 $comment2" );
            
              11 days later
              NogDog;11031579 wrote:

              I'm not familiar with the Akisment API, but if it makes sense to use that method for more than one textual field, I might just concatenate them together, e.g.:

              $akismet->setCommentContent( "$telephone $comment1 $comment2" );
              

              Thanks NogDog.

              Anyone know whether this is the right way of using Akismet, that being concatination everthing and running it through setCommentContent if it doesn't fit into the other categoriues (setCommentType, setCommentAuthor, setCommentAuthorEmail, setCommentAuthorURL) ?

              Thanks.

                Write a Reply...