


Use the strpos function in PHP to implement the function of blocking sensitive keywords, strpos keyword_PHP tutorial
The strpos function is used in PHP to implement the function of blocking sensitive keywords. The strpos keyword
Nowadays, network information supervision is very strict, especially blocking keywords. Especially in the current WEB2.0 era, almost all website content comes from netizens and can be managed by the webmaster. If you want others to prohibit publishing a certain keyword on your site, you need to do it in advance. There are various functional styles for using PHP to block keywords. For example, regular expression is the most common one. I will not list them one by one here. This article introduces the function of using the PHP function strpos to block keywords.
Things:
1. Write the keywords specifically in a text file, one per line. There is no limit to the number. Write as many as you want.
2. PHP reads the keyword text and stores it in an array
3. Traverse the keyword array and use the strpos function one by one to see if there are keywords in the content. If there are keywords, return true, if not, return false.
The PHP code is as follows:
/**
* Use strpos function to filter keywords in PHP
* Helper’s Home
*/
// Keyword filter function
function keyWordCheck($content){
// Remove white space
$content = trim($content);
//Read keyword text
$content = @file_get_contents('keyWords.txt');
// Convert to array
$arr = explode("n", $content);
// Traversal detection
for($i=0,$k=count($arr);$i // If the array element is empty, skip this loop
If($arr[$i]==''){
Continue;
}
// If a keyword is detected, return the matching keyword and terminate the operation
If(@strpos($str,$arr[$i])!==false){
//$i=$k; return $arr[$i];
}
// Return false if no keyword is detected
Return false;
}
$content = 'Here is the text content to be published. . . ';
// Filter keywords
$keyWord = keyWordCheck($content);
// Determine whether the keyword exists
if($keyWord){
echo 'The content you posted contains keywords'.$keyWord;
}else{
echo 'Congratulations! By keyword detection';
// You can perform the operation of the database to complete the release action.
}
After writing the code, I deliberately wrote a keyword content in the variable $content, and then ran it and found that no keyword was detected. The execution result was a pass, and other prohibited keywords passed.
Depressed, I started to judge whether there was something wrong.
Encoding problem? Immediately open the keyWord.txt file again with Notepad, and then save it in UTF-8 format. The result still didn't work.
Didn’t get the text content of keyWord.txt? Immediately print_r() found that it was read normally and converted into an array line by line.
So, I declared the keyword array directly in the program:
/**
* Use strpos function to filter keywords in PHP
* Helper’s Home
*/
// Keyword filter function
function keyWordCheck($content){
// Remove white space
$content = trim($content);
//Read keyword text
//$content = @file_get_contents('keyWords.txt');
// Convert to array
//$arr = explode("n", $content);
// Declare the keyword array directly in the program
$arr = array('Keyword 1','Keyword 2','Keyword 3','Keyword 4'...);
// Traversal detection
for($i=0,$k=count($arr);$i // If the array element is empty, skip this loop
If($arr[$i]==''){
Continue;
}
// If a keyword is detected, return the matching keyword and terminate the operation
If(@strpos($str,$arr[$i])!==false){
//$i=$k; return $arr[$i];
}
// Return false if no keyword is detected
Return false;
}
$content = 'Here is the content to be published, containing keyword 2';
// Filter keywords
$keyWord = keyWordCheck($content);
// Determine whether the keyword exists
if($keyWord){
echo 'The content you posted contains the keyword ['.$keyWord.']';
}else{
echo 'Congratulations! By keyword detection';
// You can perform the operation of the database to complete the release action.
}
// Program execution result: The content you posted contains the keyword [Keyword 2]
// The program is normal
If you declare an array of keywords in PHP, it works, but if you read a text file, it doesn't work. What the heck?
After adding the trim() function to filter out the blanks, the test passed. It turned out that the problem was here after messing around for a long time.
/**
* Use strpos function to filter keywords in PHP
* Helper’s Home
*/
// Keyword filter function
function keyWordCheck($content){
// Remove white space
$content = trim($content);
//Read keyword text
$content = @file_get_contents('keyWords.txt');
// Convert to array
$arr = explode("n", $content);
// Traversal detection
for($i=0,$k=count($arr);$i // If the array element is empty, skip this loop
If($arr[$i]==''){
Continue;
}
// If a keyword is detected, return the matching keyword and terminate the operation
// This time I added the trim () function
If(@strpos($str,trim($arr[$i]))!==false){
//$i=$k; return $arr[$i];
}
// Return false if no keyword is detected
Return false;
}
$content = 'Here is the text content to be published. . . ';
// Filter keywords
$keyWord = keyWordCheck($content);
// Determine whether the keyword exists
if($keyWord){
echo 'The content you posted contains keywords'.$keyWord;
}else{
echo 'Congratulations! By keyword detection';
// You can perform the operation of the database to complete the release action.
}
$test = "whello world";
print (strpos($text,"w"));return 0
print(strpos($text,"w",1 ));return 7
http://www.bkjia.com/PHPjc/867247.html

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
