PHP uses the strstr () function to block spam comments (by judging the mark)

Source: Internet
Author: User
If you have a lot of spam comments on your website, most of the spam comments contain links, because you need to add reverse links, so you can use the following tips to prevent the strstr () function with links from searching for the first appearance of a string in another string. This function returns the rest of the string (from the matching point ). If the searched string is not found, false is returned.

Syntax: strstr (string, search)

The string parameter, required. Specifies the string to be searched.
Search parameter, required. Specifies the string to be searched. If this parameter is a number, search for characters that match the ASCII value of the number.
This function is case sensitive. Use stristr () to perform case-insensitive searches ().

A simple demonstration of strstr () functions

The code is as follows:
Echo strstr ("Hello NowaMagic! "," NowaMagic ");
?>

Program running result:

NowaMagic!

Another simple example

The code is as follows:
$ Email = 'name @ example.com ';
$ Domain = strstr ($ email ,'@');
Echo $ domain; // prints @ example.com
// $ User = strstr ($ email, '@', true); // As of PHP 5.3.0
// Echo $ user; // prints name
?>

Program running result:

@ Example.com

This function can be used in many places. If you have a lot of spam comments on your website and most spam comments contain links, you can use the following tips to prevent spam comments with links.

The code is as follows:
$ Content = $ _ POST ['content'];
$ Garbage = strstr ($ content, "if ($ garbage = false)
{
// Insert database code
}
Else
{
Echo "script" alert ('Your comments cannot carry a link '); history. go (-1); script ";
}
?>

Well, that's probably the case.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.