W
W
weranda2018-02-27 11:17:11
PHP
weranda, 2018-02-27 11:17:11

What is the fastest substring search function in PHP?

Greetings
There is a small script for blocking unwanted bots by User-Agent:

// массив нежелательных значений
$bad_agents = array('python', 'alexa', 'baidu',);
// получаем user-agent
$agent = $_SERVER['HTTP_USER_AGENT'];

foreach ($bad_agents as $agent) {
    if( >>> ??? <<< ){
        die();
    }
}

I am not strong in PHP. Please tell me which specific function, namely the fastest function, to check for an unwanted user-agent to use and is it possible to improve this code or is it already optimal?
And another follow-up question: I wrote all the values ​​​​of IP addresses and user-agent to the file, but for some reason some user-agent lines are empty - why?
...
138.197.65.234    Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
45.55.138.142     Netcraft SSL Server Survey - contact [email protected]
91.224.140.214    
212.109.217.105   Needle/1.6.0 (Node.js v6.5.0; linux x64)
138.197.13.157    Python-urllib/2.7
...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2018-02-27
@weranda

php.net/manual/en/function.strpos.php

foreach ($bad_agents as $bad) {
    if(strpos($agent, $bad) !== false){
        die();
    }
}

S
Slava Vitrenko, 2018-03-07
@mxuser

// массив нежелательных значений
$bad_agents = array('python', 'alexa', 'baidu',);
// получаем user-agent
$agent = $_SERVER['HTTP_USER_AGENT'];

if(in_array($agent, $bad)){
    die();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question