PHP实现的博客欢迎提示功能、访客来源提示、催促留言、IE的推送更新提示等示例代码
分类:PHP_Python| 发布:佚名| 查看:187 | 发表时间:2014/10/24
以下代码的实现效果:
1、留言的访客显示欢迎词
2、一般访客来源提示
3、不留言潜水党(刷新大于7次,催促留言)
4、针对IE的推送更新提示
使用方法:调用相应的函数,例如welcome_msg()。
代码如下:
011 | function welcome_msg(){ |
013 | if ( $m = apply_filters( 'welcome_msg' , $string )){ |
018 | $referer = $_SERVER [ 'HTTP_REFERER' ]; |
019 | $hostinfo = parse_url ( $referer ); |
020 | $host_h = $hostinfo [ "host" ]; |
021 | $host_p = $hostinfo [ "path" ]; |
022 | $host = array ( $host_h , $host_p ); |
023 | if ( substr ( $host_h , 0, 4) == 'www.' ) |
024 | $host_h = substr ( $host_h , 4); |
025 | $host_h_url = '<a href="http://' . $host_h . '/">$host_h</a>' ; |
028 | echo "<!--您直接访问了本站!-->\n" ; |
029 | if ( $_COOKIE [ "comment_author_" . COOKIEHASH]!= "" ){ |
030 | echo 'Howdy, <strong>' . $_COOKIE [ "comment_author_" . COOKIEHASH]. '</strong>, 欢迎回来' ; |
032 | echo "您直接访问了本站! 莫非您记住了我的<strong>域名</strong>.厉害~我倍感荣幸啊 嘿嘿" ; |
036 | } elseif (preg_match( '/baidu.*/i' , $host_h )){ |
037 | echo "您通过 <strong>百度</strong> 找到了我! 厉害.你要是能够订阅我的博客那就更好了.我经常分享一些好东西哦" ; |
039 | } elseif (!preg_match( '/www\.google\.com\/reader/i' , $referer ) && preg_match( '/google\./i' , $referer )){ |
040 | echo "您通过 <strong>Google</strong> 找到了我! 厉害. 你要是能够订阅我的博客那就更好了. 我经常分享一些好东西哦" ; |
042 | } elseif (preg_match( '/search\.yahoo.*/i' , $referer ) || preg_match( '/yahoo.cn/i' , $referer )){ |
043 | echo "您通过 <strong>Yahoo</strong> 找到了我! 厉害. 你要是能够订阅我的博客那就更好了. 我经常分享一些好东西哦" ; |
046 | } elseif (preg_match( '/google\.com\/reader/i' , $referer )){ |
047 | echo "感谢你通过 <strong>Google</strong> 订阅我! 既然过来读原文了. 欢迎留言指导啊.嘿嘿 ^_^" ; |
049 | } elseif (preg_match( '/xianguo\.com\/reader/i' , $referer )){ |
050 | echo "感谢你通过 <strong>鲜果</strong> 订阅我! 既然过来读原文了. 欢迎留言指导啊.嘿嘿 ^_^" ; |
052 | } elseif (preg_match( '/zhuaxia\.com/i' , $referer )){ |
053 | echo "感谢你通过 <strong>抓虾</strong> 订阅我! 既然过来读原文了. 欢迎留言指导啊.嘿嘿 ^_^" ; |
055 | } elseif (preg_match( '/inezha\.com/i' , $referer )){ |
056 | echo "感谢你通过 <strong>哪吒</strong> 订阅我! 既然过来读原文了. 欢迎留言指导啊.嘿嘿 ^_^" ; |
058 | } elseif (preg_match( '/reader\.youdao/i' , $referer )){ |
059 | echo "感谢你通过 <strong>有道</strong> 订阅我! 既然过来读原文了. 欢迎留言指导啊.嘿嘿 ^_^" ; |
062 | echo "<!--您的访问来自本站欢迎您的光临-->" . "\n" ; |
063 | } elseif ( $_COOKIE [ "comment_author_" . COOKIEHASH]!= "" ){ |
064 | echo 'Howdy, <strong>' . $_COOKIE [ "comment_author_" . COOKIEHASH]. '</strong>欢迎从<strong>' . $host_h . '</strong>回来' ; |
066 | echo '欢迎来自<strong>' . $host_h . '</strong>的朋友. 我经常分享一些好东西哦 ^_^ 欢迎订阅我的博客.' ; |
072 | $local_info = parse_url (get_option( 'siteurl' )); |
073 | $local_host = $local_info [ 'host' ]; |
075 | if ( preg_match( "/^http:\/\/(\w+\.)?($local_host)/" , $_SERVER [ 'HTTP_REFERER' ]) != 0) return true; |
084 | function killIE( $msg ){ |
085 | if (preg_match( '/MSIE\s6/i' , $_SERVER [ 'HTTP_USER_AGENT' ])){ |
086 | if (! $_COOKIE [ 'alert_ie_visitor_' .COOKIEHASH]){ |
087 | $msg .= '<p>呃~ , 我不得不再提示一下:</p>' ; |
088 | $msg .= '<p>您正在使用古老的 Internet Explorer 浏览网页, 该浏览器不符合W3C国际标准, 本站网页可能显示不正常,或部分功能无法使用</p> |
092 | } elseif (preg_match( '/MSIE\s7/i' , $_SERVER [ 'HTTP_USER_AGENT' ])){ |
093 | if (! $_COOKIE [ 'alert_ie_visitor_' .COOKIEHASH]){ |
094 | $msg .= '<p>呃~ , 顺便提示一下:</p>' ; |
097 | } elseif (preg_match( '/MSIE\s8/i' , $_SERVER [ 'HTTP_USER_AGENT' ])){ |
098 | if (! $_COOKIE [ 'alert_ie_visitor_' .COOKIEHASH]){ |
099 | $msg .= '<p>呃~ , 顺便提示一下:</p>' ; |
100 | $msg .= '<p>很高兴看到你使用较高版本的 Internet Explorer 浏览器! 但是我还是要向您<strong>推荐: </strong> |
110 | add_filter( 'welcome_msg' , 'killIE' ); |
120 | function alert_commentator( $msg ){ |
126 | if (!isset( $_COOKIE [ 'comment_author_visit_times_' .COOKIEHASH])) |
130 | if (((int) $_COOKIE [ 'comment_author_visit_times_' .COOKIEHASH])>=6){ |
131 | if ( $comment_author = $_COOKIE [ 'comment_author_' .COOKIEHASH]) |
132 | $msg = '嗨~, ' . $comment_author . ' 我发现你来了很多次也没有留言! 欢迎发表你的看法.' ; |
134 | $msg = '新朋友? 老朋友? 我看你来了很多次却没有留言.欢迎发表你的看法.' ; |
140 | add_filter( 'welcome_msg' , 'alert_commentator' ); |
152 | function setcookie_for_alert_commentator(){ |
159 | if (!isset( $_COOKIE [ 'comment_author_visit_times_' .COOKIEHASH])){ |
160 | setcookie( 'comment_author_visit_times_' . COOKIEHASH, 1, time() + (60*60*24*300), COOKIEPATH, COOKIE_DOMAIN); |
162 | $visit_times = (int) $_COOKIE [ 'comment_author_visit_times_' .COOKIEHASH]; |
163 | setcookie( 'comment_author_visit_times_' . COOKIEHASH, ++ $visit_times , time() + (60*60*24*300), COOKIEPATH, COOKIE_DOMAIN); |
166 | if (((int) $_COOKIE [ 'comment_author_visit_times_' .COOKIEHASH])>=7){ |
168 | setcookie( 'comment_author_visit_times_' . COOKIEHASH, -2, time() + (60*60*24*300), COOKIEPATH, COOKIE_DOMAIN); |
171 | add_action( 'init' , 'setcookie_for_alert_commentator' ); |
177 | function setcookie_for_ie(){ |
178 | if (isset( $_COOKIE [ 'alert_ie_visitor_' .COOKIEHASH])) |
180 | if (preg_match( '/MSIE\s6/i' , $_SERVER [ 'HTTP_USER_AGENT' ])){ |
182 | setcookie( 'alert_ie_visitor_' .COOKIEHASH, 'ie6' ,time()+(20),COOKIEPATH,COOKIE_DOMAIN); |
183 | } elseif (preg_match( '/MSIE\s7/i' , $_SERVER [ 'HTTP_USER_AGENT' ])){ |
185 | setcookie( 'alert_ie_visitor_' .COOKIEHASH, 'ie7' ,time()+(60*60*24*3),COOKIEPATH,COOKIE_DOMAIN); |
186 | } elseif (preg_match( '/MSIE\s8/i' , $_SERVER [ 'HTTP_USER_AGENT' ])){ |
188 | setcookie( 'alert_ie_visitor_' .COOKIEHASH, 'ie8' ,time()+(60*60*24*10),COOKIEPATH,COOKIE_DOMAIN); |
191 | add_action( 'init' , 'setcookie_for_ie' ); |