PHP implementation of the blog Welcome prompt feature (very special OH) _php tips

Source: Internet
Author: User

Implementation effects of the following code:

1, message visitors show the Welcome word
2. General Visitor Source Tips
3, no message diving party (refresh more than 7 times, urging message)
4, for IE push update tips

How to: Call the appropriate function, such as welcome_msg ().

Copy Code code as follows:

<?php
/**
* Welcome words
* Previous welcome words are prompted in the upper right corner.
* Implementation principle:
* Through the $_server[' http_referer '] to judge the antecedents
* $_server["http_user_agent"] to determine the browser used by the user
* Through the $_cookie["Comment_author_". Cookiehash] Judge as a reviewer
*
*/
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= ' <a href= ' http://'. $host _h. '/' > $host _h</a> ';

Direct Input No Things
if ($referer = = "") {
echo "<!--you directly visited the site!-->\n";
if ($_cookie["Comment_author_"). Cookiehash]!= "") {
Echo ' Howdy, <strong> '. $_cookie["Comment_author_". Cookiehash]. ' </strong&gt, welcome back ';
}else{
echo "You have direct access to this site!" Did you remember my <strong> domain </strong> I feel honored.
}
Search engine
Baidu
}elseif (Preg_match ('/baidu.*/i ', $host _h)) {
echo "You through <strong> Baidu </strong> found me!" Great. If you can subscribe to my blog that would be better. I often share some good things oh ";
Google
}elseif (!preg_match ('/www\.google\.com\/reader/i ', $referer) && preg_match ('/google\./i ', $referer)) {
echo "You found me through <strong>Google</strong>!" Good. It would be better if you could subscribe to my blog. I often share some good things oh ";
Yahoo
}elseif (Preg_match ('/search\.yahoo.*/i ', $referer) | | preg_match ('/yahoo.cn/i ', $referer)) {
echo "You found me through <strong>Yahoo</strong>!" Good. It would be better if you could subscribe to my blog. I often share some good things oh ";
Reading device
Google
}elseif (Preg_match ('/google\.com\/reader/i ', $referer)) {
echo "Thank you for subscribing me through <strong>Google</strong>!" Since I have come to read the original. Welcome message Guidance AH. Hey ^_^ ";
Xianguo
}elseif (Preg_match ('/xianguo\.com\/reader/i ', $referer)) {
echo "Thank you for your <strong> fruit </strong> subscribe to me!" Since I have come to read the original. Welcome message Guidance AH. Hey ^_^ ";
Zhuaxia
}elseif (Preg_match ('/zhuaxia\.com/i ', $referer)) {
echo "Thank you for your <strong> catch shrimp </strong> subscribe to me!" Since I have come to read the original. Welcome message Guidance AH. Hey ^_^ ";
Nezha
}elseif (Preg_match ('/inezha\.com/i ', $referer)) {
echo "Thank you for your <strong> Nezha </strong> subscribe to me!" Since I have come to read the original. Welcome message Guidance AH. Hey ^_^ ";
Youdao
}elseif (Preg_match ('/reader\.youdao/i ', $referer)) {
echo "Thank you through <strong> Youdao </strong> subscribe to me!" Since I have come to read the original. Welcome message Guidance AH. Hey ^_^ ";
Myself
}elseif (self ()) {
Echo <!--your visit from our site Welcome to--> "." \ n ";
}elseif ($_cookie["Comment_author_"). Cookiehash]!= "") {
Echo ' Howdy, <strong> '. $_cookie["Comment_author_". Cookiehash]. ' </strong> welcome from <strong> $host _h. ' </strong> back ';
}else{
Echo ' Welcome from <strong> '. $host _h. ' </strong> friends. I often share some good things oh ^_^ Welcome to subscribe to my blog. '
}

}
Judgment is a function of its own
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;
}


/**
* Analysis Browser for the use of IE old version of the user push reminders
* Don't push too much, judge by Cookie
* Like a push to IE6! I hope it will be once every 20 seconds!
* @see Setcookie_for_ie ()
*/
Function Killie ($msg) {
if (Preg_match ('/msie\s6/i ', $_server[' http_user_agent ')) {
if (!$_cookie[' alert_ie_visitor_ '). Cookiehash]) {
$msg. = ' <p> er ~, I have to remind:</p> ';
$msg. = ' <p> you are using the old Internet Explorer to browse the Web page, the browser does not conform to international standards, the Web page may not appear normal, or some features can not be used </p><br/><p > If you <strong><a rel= "nofollow" title= "IE8" href= "http://www.microsoft.com/windows/internet-explorer/" > Upgrade to Internet Explorer 8</a></strong> or <strong> convert to another browser </strong&gt, this site will be able to provide you with better service. </p> ';
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. = ' <p> er ~, by the way hint:</p> ';
$msg. = ' <p> you are using the older version of Internet Explorer to browse the Web page if you <strong><a rel= "nofollow" title= ie8 "href=" http:// www.microsoft.com/windows/internet-explorer/> Upgrade to Internet Explorer 8</a></strong> or <strong> Convert to another browser </strong&gt, this site will be able to provide you with better service. </p> ';
}
}elseif (Preg_match ('/msie\s8/i ', $_server[' http_user_agent ')) {
if (!$_cookie[' alert_ie_visitor_ '). Cookiehash]) {
$msg. = ' <p> er ~, by the way hint:</p> ';
$msg. = ' <p> nice to see you using a newer Internet Explorer browser! But I still want to recommend to you <strong>: </strong><br/> fastest <strong><a rel= "nofollow" title= "Chrome href=" http://www.google.com/chrome/">Chrome</a></strong> and customization of the strongest <strong><a rel=" nofollow "title = "Firefox" href= "http://www.mozilla.com/" >Firefox</a></strong> </p> ";
}
}else{
Return
}

return $msg;

}
Add_filter (' welcome_msg ', ' Killie ');

/**
* For those who have come so many times not to comment, remind
* Create a cookie to count
* With Ajax comment function, set the calculator to-5 after comments
* This comment can have a longer period of détente
* @since 2.0.1
* @see Welcome_msg, Setcookie_for_alert_commentator
* Modified cookies Write method, here only read cookies
*/
function Alert_commentator ($msg) {
Global $user _id;
Administrators are an exception. Cannot push to admin!
if ($user _id) {
Return;//just return null;
}

if (!isset ($_cookie[' Comment_author_visit_times_ '). Cookiehash]))
return;//

Push prompts when times are >=6
Because cookies are written on init, you actually have to wait until the cookie accumulates to 7 to show the hint!
if ((int) $_cookie[' Comment_author_visit_times_ '. Cookiehash]) {>=6) {
if ($comment _author = $_cookie[' Comment_author_ '. Cookiehash])
$msg = ' Hi, '. $comment _author. ' I find you have been here many times and no messages! You are welcome to express your opinion.
Else
$msg = ' new friend '? Friends? I see you've been here many times without a message. You are welcome to express your opinion.
}else{
return;//
}

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

/**
 * set a calculator for visitors
 *
 * role:
 * But visitors have been browsing, no message counter work
 * message after the counter to 5
 *
 * @since 2.0.2
 */
Function Setcookie_for_alert_commentator () {
 if (Is_bot ())
  return;
 global $user _id;
 if ($user _id)
  return;
 //If there are no counters, write to the
 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);
 }

When the number of times is greater than 7 o'clock to stop pushing because it's been pushed 2 times.
if ((int) $_cookie[' Comment_author_visit_times_ '. Cookiehash]) {>=7) {
Set to 0 again
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 the following push upgrade notification
*/
function Setcookie_for_ie () {
if (Isset ($_cookie[' alert_ie_visitor_ '). Cookiehash]))
Return
if (Preg_match ('/msie\s6/i ', $_server[' http_user_agent ')) {
For the use of old version of IE with frequent push (cookies 5 minutes of failure)
Setcookie (' Alert_ie_visitor_ '. Cookiehash, ' IE6 ', Time () + (), cookiepath,cookie_domain);

}elseif (Preg_match ('/msie\s7/i ', $_server[' http_user_agent ')) {
Reduced push for users using IE7 (cookies are 3 days invalid)
Setcookie (' Alert_ie_visitor_ '. Cookiehash, ' IE7 ', Time () + (60*60*24*3), cookiepath,cookie_domain);

}elseif (Preg_match ('/msie\s8/i ', $_server[' http_user_agent ')) {
Do not push the use of IE8 (cookies expire 100 days)
Setcookie (' Alert_ie_visitor_ '. Cookiehash, ' IE8 ', Time () + (60*60*24*10), cookiepath,cookie_domain);
}
}
Add_action (' init ', ' setcookie_for_ie ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.