- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to detect search engine bots with PHP?
A search engine directory of the spider names can be used as a reference. Next, $_SERVER['HTTP_USER_AGENT']; can be used to check if the agent is a spider (bot).
Below is an example demonstrating the same −
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "some_bot_name")) { //other steps that need to be used }
Code explanation − The agent, along with the user agent is passed to the strtolower function, whose output in turn is passed to the strstr function. Both the user agent and the bot are compared to see if the spider is a bot or not.
Another option is shown below −
function _bot_detected() { return ( isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/bot|crawl|slurp|spider|mediapartners/i', $_SERVER['HTTP_USER_AGENT']); }
Code explanation − The preg_match function helps in finding specific patterns in the string. To the preg_match function, the bot name is passed and it is compared with the user agent that detects if the spider is a search engine bot or not.
- Related Articles
- How to use the new search engine jelly
- What is the difference between a search engine friendly and search engine optimised website?
- How to Detect User Timezone in PHP?
- Which is the best Search Engine?
- PHP – How to detect character encoding using mb_detect_encoding()
- Difference Between Search Engine and Web Browser
- PHP – Detect HTTP input character encoding with mb_http_input()
- How to use bots in customer service
- What Are The Best Search Engine Marketing (SEM) Tools?
- Detect base64 encoding in PHP?
- Simplest way to detect client locale in PHP
- Bots in RPA
- Detect language from string in PHP
- How to create a MySQL table with InnoDB engine table?
- How to create a MySQL table with MyISAM engine table?

Advertisements