PHP shows different content to visitors and crawlers

  • 2020-03-31 16:43:57
  • OfStack

I heard that this law will violate some operating principles of search engines, and may be punished by each search engine, or even delete the website.

This blog's home page and archive pages according to the article, in the form of a list when visitors click on an article to load the contents of the article. Because the content of the articles section contains a lot of words and images that need a lot of time and traffic loading. As soon as possible to show visitors the web can save a lot of visitors. For mobile phone users, loading time and flow rate are more important.

In general, the website homepage is most search engines to access page, should as far as possible to show their meaningful content, but should be displayed in the form of a list of articles, visitors and search engines can only get to the post title information. The article content or the (especially the article first sentence) is extremely important for SEO, so we try to send the content to the crawler.

Ok, we can use the User Agent to determine whether the visitor is a crawler, if so, the article will be displayed in a general form, otherwise the article list will be displayed in a list form. The following PHP methods can be used to determine whether the visitor is a crawler:
 
function is_crawler() { 
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); 
$spiders = array( 
 ' Googlebot', //Google crawler
 ' Baiduspider', //Baidu spiders
 ' Yahoo! Slurp', //Yahoo crawler
 ' YodaoBot', //Youdao crawler
 ' msnbot' //Bing crawler
//More crawler keywords
); 
foreach ($spiders as $spider) { 
$spider = strtolower($spider); 
if (strpos($userAgent, $spider) !== false) { 
return true; 
} 
} 
return false; 
} 

This is the method I used. Each crawler is sorted from high to low in order to access comments, and then the following method is used to display different contents to crawler and natural person

Related articles: