you can loop the string contain one or several words
By using the
PHP explode and then scanning the arrays of the strings.
Here's an example.
PHP Code:
$word = "spam#malware#spyware";
As you can see we have added several words into our string and we are splitting the words with the "#" symbol, and we can now use the explode function to split the string into arrays. Some will use white spaces to break the words into arrays, but as some words you may want to use can contain white spaces we will use a symbol to break up each word.
The explode function allows you to split a string into arrays by stopping and splitting at each symbol or word you tell it to do.
PHP Code:
$pieces = explode("#", $word );
Now that we have used the explode function on our $word string, we have now stored the arrays into our $pieces variable. The example below illustrates that you will get the same words as stored in your previous $word string, however, now you can call each word using an array.
PHP Code:
echo $pieces[0];
echo $pieces[1];
echo $pieces[2];
0 will be the word "spam", 1 will be the word "malware", and 2 will be the word "spyware".
We can pull out all words that we have stored using a loop. This will speed up the process when checking if a word is allowed.
PHP Code:
//Let's shorten the referer name...
$refurl = parse_url($_SERVER['HTTP_REFERER']);
$referer = $refurl['host'];
$word = "spam#malware#spyware";
$pieces = explode("#", $word );
for ($i = 0; $i<=3; $i++) { //We have 3 words in our string
if (strpos($referer, $pieces[$i])!==false){ //$i will contain the array number of the loop
//Post comment
echo "We do not accept the refering url!";
exit();
}
}
What we have told the script to do is search the string you have created, and if it encounters any of these words, we have told it to stop and block the user.
Instead of echoing out the result, we could also refer the user to another page and this can easily be done like this.
PHP Code:
//Let's shorten the referer name...
$refurl = parse_url($_SERVER['HTTP_REFERER']);
$referer = $refurl['host'];
$word = "spam#malware#spyware";
$pieces = explode("#", $word );
for ($i = 0; $i<=3; $i++) { //We have 3 words in our string
if (strpos($referer, $pieces[$i])!==false){ //$i will contain the array number of the loop
//Redirect the user
header("Location: http://www.your-redirected-page.com/");
exit();
}
}
I hope this will get you started.
Good luck!