Blog welcome prompt function implemented in PHP (very special)_PHP tutorial

WBOY
Release: 2016-07-13 10:28:56
Original
1047 people have browsed it

The implementation effect of the following code:

1. Welcome words are displayed for visitors who leave messages
2. General visitor source tips
3. No-leave divers (refresh more than 7 times, urge to leave messages)
4. Push update tips for IE

Usage: Call the corresponding function, such as welcome_msg().

Copy code The code is as follows:

/**
* 欢迎词
* 以前的欢迎词在右上角提示.
* 实现原理:
* 通过$_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');

 

 

/**
* Set up a calculator for visitors
*
* Function:
* However, the visitor keeps browsing and the counter does not work
* After leaving a message, the counter will be reduced to -5
*
* @since 2.0.2
*/
function setcookie_for_alert_commentator(){
if(is_bot())
return;
global $user_ID;
if($user_ID)
return;
//If there is no counter, write
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);
}

//Stop pushing when the number is greater than 7 because it has been pushed 2 times in a row
if(((int)$_COOKIE['comment_author_visit_times_'.COOKIEHASH])>=7){
//Settings Reset to 0
setcookie('comment_author_visit_times_'. COOKIEHASH, -2, time() + (60*60*24*300), COOKIEPATH, COOKIE_DOMAIN);
}
}
add_action( 'init', 'setcookie_for_alert_commentator');

/**
* Set different cookies for different versions of ie
*
* For future push upgrade notifications
*/
function setcookie_for_ie(){
if(isset($_COOKIE['alert_ie_visitor_'.COOKIEHASH]))
return;
if(preg_match(' /MSIEs6/i', $_SERVER['HTTP_USER_AGENT'])){
//For those who use the old version of ie with frequent push (cookies expire in 5 minutes)
setcookie('alert_ie_visitor_'.COOKIEHASH,'ie6', time()+(20),COOKIEPATH,COOKIE_DOMAIN);

}elseif(preg_match('/MSIEs7/i', $_SERVER['HTTP_USER_AGENT'])){
//Reduce push for users using ie7 (cookies expire in 3 days)
setcookie('alert_ie_visitor_ '.COOKIEHASH,'ie7',time()+(60*60*24*3),COOKIEPATH,COOKIE_DOMAIN);

}elseif(preg_match('/MSIEs8/i', $_SERVER['HTTP_USER_AGENT'])){
//Try not to push for those using ie8 (cookies expire in 100 days)
setcookie(' alert_ie_visitor_'.COOKIEHASH,'ie8',time()+(60*60*24*10),COOKIEPATH,COOKIE_DOMAIN);
}
}
add_action('init', 'setcookie_for_ie');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/780487.htmlTechArticleThe implementation effect of the following code: 1. Visitors who leave messages display a welcome message 2. General visitor source tips 3. No messages Diving party (refresh more than 7 times, urge to leave a message) 4. Push updates for IE...
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template