In PHP Strpos is the first place to find the string, if there is a return ture or related to the specific number, no return 0 or FALSE, but I want to use it to do a wordpress keyword blacklist anti-spam comments found some problems, below we look.
Modify the theme comments-ajax.php file
In the comments-ajax.php file in the theme directory, about 60 lines (just get the $_post[' author ' in the user-submitted comment form). Then add the following code to the file:
code is as follows |
copy code |
/* * @Author: VFHK Y September 21, 2013 22:13 * @Variable string $word: The keywords in the blacklist, the user can increase or decrease their own rules by themselves * @Variable string $comment _author: User-submitted $_ post[' author ' field value, which represents the nickname * @Variable string $comment _content: User-submitted $_post[' Comment ' field value, which represents the comment **/ $words = " Com,cn,info,net,www,http,cc,host, agent, Mobile, power, country, port, device, service, medicine, fertilizer, medicine, agriculture, letter, loan, day, surplus, net, ticket, domain, PIN, yellow, Division, Enterprise, machine, rent, person, money, set, buy, broadcast "; $word = Explode (', ', $words); $num = count ($word); for ($i =0; $i < $num; $i + +) { if (Strpos ($comment _author, $word [$i],0) | | strpos ($comment _content, $word [$i],0) { Err (' Ads must be deleted, thank you for understanding! ')); break; } } |
4 PostScript
Through this simple code, we implemented the submission of comments entered by the user's nickname, the comment content was blacklisted the keyword check. Once matched to any of the above words, such as the advent of www, then prompted the user "ads must be deleted, thank you understand!", the effect as shown. This is also to blog on an insurance, enhanced WordPress anti-spam Review immunity, but also non-plug-in method to achieve yo!
The above is not a problem, but the morning @ bad boy shoes an evil test, found the previous article code bug. Come back from work at night, carefully read the code, found that their own strpos function of the one-sided understanding, so make a note mark.
2 prototype of the Strpos function
I believe that you are not unfamiliar with the Strpos function, often in the processing of strings can see its figure. The Strpos function prototype is:
/*
* @Para string $source: Find in this string [*]
* @Para Mixed $target: The string to find, if not a string, to be converted to an integer and treated as a sequential value of the character [*]
* @Para int $offset: The starting position of the lookup
* @Return Int/boolean: Success returns to the first occurrence of the position; Failed to return FALSE value
**/
int Strpos (string $source, mixed $target [, int $offset = 0]);
3 Simple test of strpos function
After understanding the prototype of the Strpos function, let's look at a simple test code.
code as follows |
copy code |
/* * @Author: Vfhky September 21, 2013 20:35 * @Description: Through two different test variables $test_1 and $test_2 direct hit key **/ $words = "Com,cn,info,net,www,http,cc,host, agent, mobile, electricity, country, port, day, purchase"; $word = Explode (', ', $words); $num = count ($word); $test _1 = "Buy tt"; for ($i =0; $i < $num; $i + +) { if (Strpos ($test _1, $word [$i],0)} { Echo ' advertising must be deleted, thanks for understanding! '; Break } } echo "
----------This is $test _1 END----------
";
$test _2 = "bad bad buy tt"; for ($i =0; $i < $num; $i + +) { if (Strpos ($test _2, $word [$i],0)} { Echo ' advertising must be deleted, thanks for understanding! '; Break } } echo "
----------This is $test _2 END----------
"; ?> |
The test results are as follows:
Discussion on WordPress anti-spam Comment: all strpos function
Analysis of test results of 4 Strpos function
The above code has two different test variables $test_1 and $test_2, and both contain the keywords in the blacklist: Buy. However, the test results show that $test _1 variable is not effectively masked, but the variable $test_2 is prompted to include the advertising word. The mystery lies in the location of the word "buy" in variable $test_1 and $test_2! When the keyword "buy" appears at the front ($test _1), Strpos ($test _1, $word [$i],0) function results in 0 because the "buy" word is at the front of the string "Purchase TT". Then the IF statement in the for loop becomes the if (0) {}, which is not considered a spam comment, which creates a bug. The following are the continuation of the Strpos function and the use of PHP regular expressions, two ways to implement the "WordPress keyword blacklist: anti-spam review and upgrade."
5.1 Correct the bug by using the Strpos function correctly
code is as follows |
copy code |
/* * @Author: VFHK Y September 24, 2013 20:06 * @Description: Correct use of the Strpos function to resolve the bug in the previous article code **/ $words = "Com,cn,info,net,www,http, Cc,host, agent, mobile, electricity, country, port, device, service, medicine, fertilizer, medicine, agriculture, letter, loan, day, buy, broadcast "; $word = Explode (', ', $words); $num = count ($word); for ($i =0; $i < $num; $i + +) { if (Strpos ($comment _author, $word [$i],0)!== false) | | (Strpos ($comment _content, $word [$i],0)!== false)) { Err (' Ads must be deleted, thank you for understanding! '). break; } } |
5.2 Fix bugs with PHP regular expressions
code is as follows |
copy code |
/* * @Author: Vfhky September 24, 2013 20:06 * @Description: Use PHP regular expressions to fix bugs, implement "WordPress keyword blacklist: anti-spam Review and upgrade (non-plug-in)" **/ $words = "com, Cn,info,net,www,http,cc,host, agent, mobile, electricity, country, port, device, service, medicine, fertilizer, medicine, agriculture, letter, loan, day, buy, broadcast "; $word = Explode (', ', $words); $num = count ($word); for ($i =0; $i < $num; $i + +) { if (Preg_match ("/$word [$i]/i", $comment _author) | | preg_match ("/$word [$i]/i", $ comment_content) { Err (' Advertising must be deleted, thank you for understanding! ')); break; } } |
6 Important reminders of function strpos
One thing to note about using the Strpos function is that it may return a Boolean value of false, but it may also return a non-Boolean value that is equivalent to false.
For example, return integer 0, floating-point value 0.0, empty string, string "0", not including an array of any elements, objects that do not include any member variables, special type NULL, and so on.
Therefore, the return value of this function should be tested using the identity operator "= = =" of the type that will examine the returned value instead of using the simple equal sign "= =".
7Update 2013.09.26 22:27
You can use arrays instead of strings as reminders of the star of the galaxy, and the execution efficiency should be similar.
7.1 Fix bug with Strpos function + array
The code is as follows |
Copy Code |
$words = Array ("com", "cn", "info", "net", "www", "http", "CC", "host", "Proxy", "mobile", "electricity", "country", "Port", "buy"); $num = count ($words); for ($i =0; $i < $num; $i + +) { if (Strpos ($comment _author, $words [$i],0)!== false | | Strpos ($comment _content, $words [$i],0)!== false) { Err (' Ads must be deleted, thank you for understanding! '); Break } } |
7.2 Fix bugs with regular + array
The code is as follows |
Copy Code |
$words = Array ("com", "cn", "info", "net", "www", "http", "CC", "host", "Proxy", "mobile", "electricity", "country", "Port", "buy"); $num = count ($words); for ($i =0; $i < $num; $i + +) { if (Preg_match ("/$words [$i]/i", $comment _author) | | preg_match ("/$words [$i]/i", $comment _content)) { Err (' Ads must be deleted, thank you for understanding! '); Break } } |
http://www.bkjia.com/PHPjc/633054.html www.bkjia.com true http://www.bkjia.com/PHPjc/633054.html techarticle in PHP Strpos is the first place to find the string, if there is a return ture or related to the specific number, no return 0 or FALSE, but I want to use it to do a wordpress keyword Black name ...