Contents Previous Next

12.1 Generating Anti-Spam challenge

There are two basic alternatives on how to generate the content of the anti-spam image

  1. Submit a string that should be used
  2. Automatically generate a random string. If this alternative is chosen then the user of the library should save the created string and compare it to what the user enters.

In order to write a script to generate a new challenge there are four steps to be completed.

First include the library file jpgraph_antispam.php. Note that there is no need to include the "jpgraph.php" library since all functionality is included in this library file.

  require_once "jpgraph_antispam.php";

Secondly a new instance of the class AntiSpam must be created

 $spam = new  AntiSpam();

Thirdly the string to be used in the challenge must be specified. To automatically generate a suitable string use

  // The argument determines the length of the generated string
$chars $spam-> Rand(5);

If instead the string to be used should be specified this string should be specified in the initial creation of the AntiSpam() or by calling the Set() method as in

 $spam-> Set("aui8k");

Please note that in order to minimize the risk for confusion the letters 'O' and the number '0' (zero) is not allowed since they are too alike and can be mistaken for each other.

The final and fourth step is to output the image with a call the method Stroke() on the created instance of the AntiSpam class.

 if( $spam->Stroke () === false  ) {
    die(
"Illegal or no data to plot");
}

Note that we have put a guard around the output since in the case of an error this method will result a boolean false value. As with the other graph types it is possible to write the generated image to a file by submitting a file name as an argument to Stroke().


Contents Previous Next