Home php教程 php手册 PHP实现的博客欢迎提示功能(很特别哦)

PHP实现的博客欢迎提示功能(很特别哦)

Jun 13, 2016 am 09:34 AM
php blog

以下代码的实现效果:

1、留言的访客显示欢迎词
2、一般访客来源提示
3、不留言潜水党(刷新大于7次,催促留言)
4、针对IE的推送更新提示

使用方法:调用相应的函数,例如welcome_msg()。

复制代码 代码如下:


/**
 * 欢迎词
 * 以前的欢迎词在右上角提示.
 * 实现原理:
 * 通过$_SERVER['HTTP_REFERER']判断来路
 * 通过$_SERVER["HTTP_USER_AGENT"]判断用户使用的浏览器
 * 通过$_COOKIE["comment_author_" . COOKIEHASH]判断为评论者
 *
 */
function welcome_msg(){


 if($m = apply_filters('welcome_msg',$string)){
  echo $m;
  return;
 }
 global $referer;
 $referer=$_SERVER['HTTP_REFERER'];
 $hostinfo=parse_url($referer);
 $host_h=$hostinfo["host"];
 $host_p=$hostinfo["path"];
 $host=array($host_h,$host_p);
 if(substr($host_h, 0, 4) == 'www.')
  $host_h = substr($host_h, 4);
  $host_h_url='$host_h';

    //直接输入 没有东西
  if($referer==""){
   echo "\n";
   if($_COOKIE["comment_author_" . COOKIEHASH]!=""){
    echo 'Howdy, '.$_COOKIE["comment_author_" . COOKIEHASH].', 欢迎回来';
   }else{
    echo "您直接访问了本站!  莫非您记住了我的域名.厉害~我倍感荣幸啊 嘿嘿";
   } 
  //搜索引擎
   //baidu
  }elseif(preg_match('/baidu.*/i',$host_h)){
   echo "您通过 百度 找到了我! 厉害.你要是能够订阅我的博客那就更好了.我经常分享一些好东西哦";
   //google
  }elseif(!preg_match('/www\.google\.com\/reader/i',$referer) && preg_match('/google\./i',$referer)){
   echo "您通过 Google 找到了我! 厉害. 你要是能够订阅我的博客那就更好了. 我经常分享一些好东西哦";
   //yahoo
  }elseif(preg_match('/search\.yahoo.*/i',$referer) || preg_match('/yahoo.cn/i',$referer)){
   echo "您通过 Yahoo 找到了我! 厉害. 你要是能够订阅我的博客那就更好了. 我经常分享一些好东西哦";
  //阅读器
   //google
  }elseif(preg_match('/google\.com\/reader/i',$referer)){
   echo "感谢你通过 Google 订阅我!  既然过来读原文了. 欢迎留言指导啊.嘿嘿 ^_^";
   //xianguo
  }elseif(preg_match('/xianguo\.com\/reader/i', $referer)){
   echo "感谢你通过 鲜果 订阅我!  既然过来读原文了. 欢迎留言指导啊.嘿嘿 ^_^";
   //zhuaxia
  }elseif(preg_match('/zhuaxia\.com/i', $referer)){
   echo "感谢你通过 抓虾 订阅我!  既然过来读原文了. 欢迎留言指导啊.嘿嘿 ^_^";
   //哪吒
  }elseif(preg_match('/inezha\.com/i', $referer)){
   echo "感谢你通过 哪吒 订阅我!  既然过来读原文了. 欢迎留言指导啊.嘿嘿 ^_^";
   //有道
  }elseif(preg_match('/reader\.youdao/i', $referer)){
   echo "感谢你通过 有道 订阅我!  既然过来读原文了. 欢迎留言指导啊.嘿嘿 ^_^";
   //自己 
  }elseif(self()){
   echo ""."\n";
  }elseif($_COOKIE["comment_author_" . COOKIEHASH]!=""){
   echo 'Howdy, '.$_COOKIE["comment_author_" . COOKIEHASH].'欢迎从'.$host_h.'回来';
  }else{
   echo '欢迎来自'. $host_h.'的朋友. 我经常分享一些好东西哦 ^_^  欢迎订阅我的博客.';
  }

}
//判断是自己的函数
function self(){
 $local_info = parse_url(get_option('siteurl'));
    $local_host = $local_info['host'];
 //check self
 if ( preg_match("/^http:\/\/(\w+\.)?($local_host)/",$_SERVER['HTTP_REFERER']) != 0) return true;
}


 /**
  * 分析浏览器 对于使用IE老版本的用户推送提醒
  * 不要过分推送, 根据cookie判断
  * 比如 对IE6的推送! 我希望是每隔20秒要有一次!
  * @see setcookie_for_ie()
  */
function killIE($msg){ 
 if(preg_match('/MSIE\s6/i', $_SERVER['HTTP_USER_AGENT'])){
  if(!$_COOKIE['alert_ie_visitor_'.COOKIEHASH]){
   $msg .= '

呃~ , 我不得不再提示一下:

';
   $msg .= '

您正在使用古老的 Internet Explorer 浏览网页, 该浏览器不符合W3C国际标准, 本站网页可能显示不正常,或部分功能无法使用


如果您升级到 Internet Explorer 8转换到另一个浏览器, 本站将能为您提供更好的服务.

';
   //add_action('init', 'setcookie_for_alert_ie_visitor');
  }
 }elseif(preg_match('/MSIE\s7/i', $_SERVER['HTTP_USER_AGENT'])){
  if(!$_COOKIE['alert_ie_visitor_'.COOKIEHASH]){
   $msg .= '

呃~ , 顺便提示一下:

';
   $msg .= '

您正在使用旧版本的 Internet Explorer 版本浏览网页,如果您升级到 Internet Explorer 8转换到另一个浏览器, 本站将能为您提供更好的服务.

';
  }
 }elseif(preg_match('/MSIE\s8/i', $_SERVER['HTTP_USER_AGENT'])){
  if(!$_COOKIE['alert_ie_visitor_'.COOKIEHASH]){
   $msg .= '

呃~ , 顺便提示一下:

';
   $msg .= '

很高兴看到你使用较高版本的 Internet Explorer 浏览器! 但是我还是要向您推荐:
速度最快的 Chrome 和定制性最强的 Firefox

';
  }
 }else{
  return;
 }

return $msg;

 }
add_filter('welcome_msg','killIE');

/**
 * 对于来了很多次也不评论的家伙提醒
 * 创建一个cookie用来计数
 * 结合ajax评论函数,评论后将计算器设置为-5
 * 这样评论后可以有个较长的缓和期
 * @since 2.0.1
 * @see welcome_msg, setcookie_for_alert_commentator
 * 修改了cookies的写入方法,这里只读取cookies
 */
function alert_commentator($msg){
 global $user_ID;
 //管理员是个例外.不能对管理员推送!
 if($user_ID){
  return;//just return null;
 }

 if(!isset($_COOKIE['comment_author_visit_times_'.COOKIEHASH]))
  return;//

 //当次数>=6次时 推送提示
 //由于在init上写入cookie所以实际上要等cookie累加到7是才显示提示!
 if(((int)$_COOKIE['comment_author_visit_times_'.COOKIEHASH])>=6){
  if($comment_author = $_COOKIE['comment_author_'.COOKIEHASH])
   $msg = '嗨~,  '.$comment_author.' 我发现你来了很多次也没有留言! 欢迎发表你的看法.';
  else
   $msg = '新朋友? 老朋友? 我看你来了很多次却没有留言.欢迎发表你的看法.';
 }else{
  return;//
 }

 return $msg;
}
add_filter('welcome_msg','alert_commentator');

 

 

/**
 * 给访客设置一个计算器
 *
 * 作用:
 * 不过访客一直浏览,不留言计数器工作
 * 留言后将计数器归为-5
 *
 * @since 2.0.2
 */
function setcookie_for_alert_commentator(){
 if(is_bot())
  return;
 global $user_ID;
 if($user_ID)
  return;
 //如果没有计数器,写入
 if(!isset($_COOKIE['comment_author_visit_times_'.COOKIEHASH])){
  setcookie('comment_author_visit_times_'. COOKIEHASH, 1, time() + (60*60*24*300), COOKIEPATH, COOKIE_DOMAIN);
 }else{
  $visit_times = (int)$_COOKIE['comment_author_visit_times_'.COOKIEHASH];
  setcookie('comment_author_visit_times_'. COOKIEHASH, ++$visit_times, time() + (60*60*24*300), COOKIEPATH, COOKIE_DOMAIN);
 }

 //当次数大于7时 停止推送 因为连续推送了2次了
 if(((int)$_COOKIE['comment_author_visit_times_'.COOKIEHASH])>=7){
  //设置为0 重来
  setcookie('comment_author_visit_times_'. COOKIEHASH, -2, time() + (60*60*24*300), COOKIEPATH, COOKIE_DOMAIN);
 }
}
add_action('init', 'setcookie_for_alert_commentator');

/**
 * 针对ie不同版本设置不同的cookie
 *
 * 为了后面的推送升级通知
 */
function setcookie_for_ie(){
 if(isset($_COOKIE['alert_ie_visitor_'.COOKIEHASH]))
  return;
 if(preg_match('/MSIE\s6/i', $_SERVER['HTTP_USER_AGENT'])){
  //对于使用古老版ie用频繁推送 (cookies 5分钟失效)
  setcookie('alert_ie_visitor_'.COOKIEHASH,'ie6',time()+(20),COOKIEPATH,COOKIE_DOMAIN);

 }elseif(preg_match('/MSIE\s7/i', $_SERVER['HTTP_USER_AGENT'])){
  //对于使用ie7的用户减少推送 (cookies 3天失效)
  setcookie('alert_ie_visitor_'.COOKIEHASH,'ie7',time()+(60*60*24*3),COOKIEPATH,COOKIE_DOMAIN);

 }elseif(preg_match('/MSIE\s8/i', $_SERVER['HTTP_USER_AGENT'])){
  //对于使用ie8的用尽量不要推送 (cookies 100天失效)
  setcookie('alert_ie_visitor_'.COOKIEHASH,'ie8',time()+(60*60*24*10),COOKIEPATH,COOKIE_DOMAIN);
 }
}
add_action('init', 'setcookie_for_ie');

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles