Until a number of days ago there was a small box on the site that greeted visitors and automagicaly knew where the visitor was coming from.
Since then it has disappeared as I have relegated it to my plugin projects and working on it from that basis. Though I know it would be of interest to some people so below is the ‘main’ code i’m using.
You may ask why would I need it as there are already some plugins that do this. The answer is they rely on javascript, and while javascript is brilliant, but in my opinion PHP is a lot less dependant on user factors. What I mean is that I use no-script addon for firefox, and it normally blocks JS and using PHP instead would sill enable it to still work with users such as me.
So without delay here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 | //set id
global $id;
$refer=$_SERVER['HTTP_REFERER'];
$title=the_title('','',false);
$domain=$matches[0];
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i',
"$refer", $matches);
$host = $matches[1];
// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches); |
This first chunk of code is very simple, what it does is check the ‘refer’ of the visitor (i.e. what site it is coming from) and then saves the first part of the url as a variable (the ’site.com’ bit stripping anything after it).
This is useful as I will be using it as a variable later in the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //the message
switch ($domain) {
case "":
echo "Hello! <br>";
if(isset($_COOKIE['comment_author_'.COOKIEHASH])) {
$lastCommenter = $_COOKIE['comment_author_'.COOKIEHASH];
echo . $lastCommenter ."!";
} else {
echo " Guest from <br>";
}
echo "{$matches[0]}\n";
echo "hope you enjoy : " ;
echo $title;
echo "<br> Feel Free To Bookmark it";
break; |
This is where the ‘magic’ occurs. Currently the ‘case’ is blank signifying a blank referrer, signifying a direct visitor. Once it has determined the referrer it then does another check:
1 2 3 4 5 6 | if(isset($_COOKIE['comment_author_'.COOKIEHASH])) {
$lastCommenter = $_COOKIE['comment_author_'.COOKIEHASH];
echo . $lastCommenter ."!";
} else {
echo " Guest from <br>";
} |
Namely if we already know the visitor so we can call him by his name and make it more personal. (credit to WPrecipes)
If we do not know the person it displays a generic message.
The benefit of using ‘case’ in the code is that we can amend it allowing personalised options for each domain. The can enable a ‘call to action’ if visitors come from certain domains such as for example:
1 2 3 4 5 6 | case "stumbleupon.com":
echo "Hello! <br>";
echo "{$matches[0]}\n";
echo " visitor<br>";
echo "If you Like it <a href =http://www.stumbleupon.com/submit?url=PERMALINK&title=TITLE"> Stumble it! </a>;
break; |
The first line signifies what domain the script should look out for, if it finds the said domain it runs the ‘case’.
1 | echo "{$matches[0]}\n"; |
This displays the domain name of the referral site.
1 | echo "If you Like it <a href =http://www.stumbleupon.com/submit?url=PERMALINK&title=TITLE"> Stumble it! </a>; |
This line gets the ‘link’ of your post and gets it ready for submission to the social media site, in the example the case was ’stumbleupon’ so the permalink is for stumbleupon submission.
Ofc you are not limited to just stumbleupon below is a list of other social media sites you can use, but remember to add the following at the end.
1 | break; |
This tells the script to stop working once it has found the domain that is relevant.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | 'BarraPunto' 'url' => 'http://barrapunto.com/submit.pl?subj=TITLE&story=PERMALINK', 'blinkbits' 'url' => 'http://www.blinkbits.com/bookmarklets/save.php?v=1&source_url=PERMALINK&title=TITLE&body=TITLE', 'BlinkList' 'url' => 'http://www.blinklist.com/index.php?Action=Blink/addblink.php&Url=PERMALINK&Title=TITLE', 'BlogMemes' 'url' => 'http://www.blogmemes.net/post.php?url=PERMALINK&title=TITLE', 'BlogMemes Fr 'url' => 'http://www.blogmemes.fr/post.php?url=PERMALINK&title=TITLE', 'BlogMemes Sp' 'url' => 'http://www.blogmemes.com/post.php?url=PERMALINK&title=TITLE', 'BlogMemes Cn' 'url' => 'http://www.blogmemes.cn/post.php?url=PERMALINK&title=TITLE', 'BlogMemes Jp' 'url' => 'http://www.blogmemes.jp/post.php?url=PERMALINK&title=TITLE', 'blogmarks' 'url' => 'http://blogmarks.net/my/new.php?mini=1&simple=1&url=PERMALINK&title=TITLE', 'Blogosphere News' 'url' => 'http://www.blogospherenews.com/submit.php?url=PERMALINK&title=TITLE', 'Blogsvine' 'url' => 'http://blogsvine.com/submit.php?url=PERMALINK', 'blogtercimlap' 'url' => 'http://cimlap.blogter.hu/index.php?action=suggest_link&title=TITLE&url=PERMALINK', 'Blue Dot' 'url' => 'http://bluedot.us/Authoring.aspx?u=PERMALINK&title=TITLE', 'Book.mark.hu' 'url' => 'http://book.mark.hu/bookmarks.php/?action=add&address=PERMALINK%2F&title=TITLE', 'Bumpzee' 'url' => 'http://www.bumpzee.com/bump.php?u=PERMALINK', 'co.mments' 'url' => 'http://co.mments.com/track?url=PERMALINK&title=TITLE', 'connotea' 'url' => 'http://www.connotea.org/addpopup?continue=confirm&uri=PERMALINK&title=TITLE', 'del.icio.us' 'url' => 'http://del.icio.us/post?url=PERMALINK&title=TITLE', 'De.lirio.us' 'url' => 'http://de.lirio.us/rubric/post?uri=PERMALINK;title=TITLE;when_done=go_back', 'Design Float' 'url' => 'http://www.designfloat.com/submit.php?url=PERMALINK&title=TITLE', 'Digg' 'url' => 'http://digg.com/submit?phase=2&url=PERMALINK&title=TITLE', 'DotNetKicks' 'url' => 'http://www.dotnetkicks.com/kick/?url=PERMALINK&title=TITLE', 'DZone' 'url' => 'http://www.dzone.com/links/add.html?url=PERMALINK&title=TITLE', 'eKudos' 'url' => 'http://www.ekudos.nl/artikel/nieuw?url=PERMALINK&title=TITLE', 'Facebook' 'url' => 'http://www.facebook.com/sharer.php?u=PERMALINK&t=TITLE', 'Fark' 'url' => 'http://cgi.fark.com/cgi/fark/edit.pl?new_url=PERMALINK&new_comment=TITLE&new_comment=BLOGNAME&linktype=Misc', // To post to a different category, see the drop-down box labelled "Link Type" at // http://cgi.fark.com/cgi/fark/submit.pl for a complete list 'feedmelinks' 'url' => 'http://feedmelinks.com/categorize?from=toolbar&op=submit&url=PERMALINK&name=TITLE', 'Furl' 'url' => 'http://www.furl.net/storeIt.jsp?u=PERMALINK&t=TITLE', 'Fleck' 'url' => 'http://extension.fleck.com/?v=b.0.804&url=PERMALINK', 'Global Grind' 'url' => 'http://globalgrind.com/submission/submit.aspx?url=PERMALINK&type=Article&title=TITLE' 'Google' 'url' => 'http://www.google.com/bookmarks/mark?op=edit&bkmk=PERMALINK&title=TITLE' 'Gwar' 'url' => 'http://www.gwar.pl/DodajGwar.html?u=PERMALINK', 'Haohao' 'url' => 'http://www.haohaoreport.com/submit.php?url=PERMALINK&title=TITLE', 'HealthRanker' 'url' => 'http://healthranker.com/submit.php?url=PERMALINK&title=TITLE', 'Hemidemi' 'url' => 'http://www.hemidemi.com/user_bookmark/new?title=TITLE&url=PERMALINK', 'IndiaGram' 'url' => 'http://www.indiagram.com/mock/bookmarks/desitrain?action=add&address=PERMALINK&title=TITLE', 'IndianPad' 'url' => 'http://www.indianpad.com/submit.php?url=PERMALINK', 'Internetmedia' 'url' => 'http://internetmedia.hu/submit.php?url=PERMALINK' 'kick.ie' 'url' => 'http://kick.ie/submit/?url=PERMALINK&title=TITLE', 'Kirtsy' 'url' => 'http://www.kirtsy.com/submit.php?url=PERMALINK&title=TITLE', 'laaik.it' 'url' => 'http://laaik.it/NewStoryCompact.aspx?uri=PERMALINK&headline=TITLE∓cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12', 'LinkArena' 'url' => 'http://linkarena.com/bookmarks/addlink/?url=PERMALINK&title=TITLE', 'LinkaGoGo' 'url' => 'http://www.linkagogo.com/go/AddNoPopup?url=PERMALINK&title=TITLE', 'LinkedIn' 'url' => 'http://www.linkedin.com/shareArticle?mini=true&url=PERMALINK&title=TITLE&source=BLOGNAME&summary=EXCERPT', 'Linkter' 'url' => 'http://www.linkter.hu/index.php?action=suggest_link&url=PERMALINK&title=TITLE', 'Live' 'url' => 'https://favorites.live.com/quickadd.aspx?marklet=1&url=PERMALINK&title=TITLE', 'Ma.gnolia' 'url' => 'http://ma.gnolia.com/bookmarklet/add?url=PERMALINK&title=TITLE', 'Meneame' 'url' => 'http://meneame.net/submit.php?url=PERMALINK', 'MisterWong' 'url' => 'http://www.mister-wong.com/addurl/?bm_url=PERMALINK&bm_description=TITLE&plugin=soc', 'MisterWong.DE' 'url' => 'http://www.mister-wong.de/addurl/?bm_url=PERMALINK&bm_description=TITLE&plugin=soc', 'Mixx' 'url' => 'http://www.mixx.com/submit?page_url=PERMALINK&title=TITLE', 'muti' 'url' => 'http://www.muti.co.za/submit?url=PERMALINK&title=TITLE', 'MyShare' 'url' => 'http://myshare.url.com.tw/index.php?func=newurl&url=PERMALINK&desc=TITLE', 'N4G' 'url' => 'http://www.n4g.com/tips.aspx?url=PERMALINK&title=TITLE', 'NewsVine' 'url' => 'http://www.newsvine.com/_tools/seed&save?u=PERMALINK&h=TITLE', 'Netvouz' 'url' => 'http://www.netvouz.com/action/submitBookmark?url=PERMALINK&title=TITLE&popup=no', 'NuJIJ' 'url' => 'http://nujij.nl/jij.lynkx?t=TITLE&u=PERMALINK', 'PlugIM' 'url' => 'http://www.plugim.com/submit?url=PERMALINK&title=TITLE', 'PopCurrent' 'url' => 'http://popcurrent.com/submit?url=PERMALINK&title=TITLE&rss=RSS', 'Pownce' 'url' => 'http://pownce.com/send/link/?url=PERMALINK&note_body=TITLE&note_to=all' 'ppnow' 'url' => 'http://www.ppnow.net/submit.php?url=PERMALINK', 'Propeller' 'url' => 'http://www.propeller.com/submit/?U=PERMALINK&T=TITLE', 'Ratimarks' 'url' => 'http://ratimarks.org/bookmarks.php/?action=add&address=PERMALINK&title=TITLE', 'RawSugar' 'url' => 'http://www.rawsugar.com/tagger/?turl=PERMALINK&tttl=TITLE', 'Rec6' 'url' => 'http://www.syxt.com.br/rec6/link.php?url=PERMALINK&=TITLE', 'Reddit' 'url' => 'http://reddit.com/submit?url=PERMALINK&title=TITLE', 'SalesMarks' url' => 'http://salesmarks.com/submit?edit[url]=PERMALINK&edit[title]=TITLE', 'Scoopeo' 'url' => 'http://www.scoopeo.com/scoop/new?newurl=PERMALINK&title=TITLE', 'scuttle' 'url' => 'http://www.scuttle.org/bookmarks.php/maxpower?action=add&address=PERMALINK&title=TITLE', 'Segnalo' 'url' => 'http://segnalo.alice.it/post.html.php?url=PERMALINK&title=TITLE', 'Shadows' 'url' => 'http://www.shadows.com/features/tcr.htm?url=PERMALINK&title=TITLE', 'Simpy' 'url' => 'http://www.simpy.com/simpy/LinkAdd.do?href=PERMALINK&title=TITLE', 'Slashdot' 'url' => 'http://slashdot.org/bookmark.pl?title=TITLE&url=PERMALINK', 'Smarking' 'url' => 'http://smarking.com/editbookmark/?url=PERMALINK&title=TITLE', 'Socialogs' 'url' => 'http://socialogs.com/add_story.php?story_url=PERMALINK&story_title=TITLE', 'Spurl' 'url' => 'http://www.spurl.net/spurl.php?url=PERMALINK&title=TITLE', 'SphereIt' 'url' => 'http://www.sphere.com/search?q=sphereit:PERMALINK&title=TITLE', 'Sphinn' 'url' => 'http://sphinn.com/submit.php?url=PERMALINK&title=TITLE', 'Taggly' 'url' => 'http://taggly.com/bookmarks.php/pass?action=add&address=', 'Technorati' 'url' => 'http://technorati.com/faves?add=PERMALINK', 'TailRank' 'url' => 'http://tailrank.com/share/?text=&link_href=PERMALINK&title=TITLE', 'ThisNext' 'url' => 'http://www.thisnext.com/pick/new/submit/sociable/?url=PERMALINK&name=TITLE', 'TwitThis' 'url' => 'http://twitthis.com/twit?url=PERMALINK', 'Upnews' 'url' => 'http://www.upnews.it/submit?url=PERMALINK&title=TITLE', 'Webnews.de' 'url' => 'http://www.webnews.de/einstellen?url=PERMALINK&title=TITLE', 'Webride' 'url' => 'http://webride.org/discuss/split.php?uri=PERMALINK&title=TITLE', 'Wikio' 'url' => 'http://www.wikio.com/vote?url=PERMALINK', 'Wikio FR' 'url' => 'http://www.wikio.fr/vote?url=PERMALINK', 'Wikio IT' 'url' => 'http://www.wikio.it/vote?url=PERMALINK', 'Wists' 'url' => 'http://wists.com/s.php?c=&r=PERMALINK&title=TITLE', 'Wykop' 'url' => 'http://www.wykop.pl/dodaj?url=PERMALINK', 'Xerpi' 'url' => 'http://www.xerpi.com/block/add_link_from_extension?url=PERMALINK&title=TITLE', 'YahooMyWeb' 'url' => 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=PERMALINK&=TITLE', 'Yigg' 'url' => 'http://yigg.de/neu?exturl=PERMALINK&exttitle=TITLE', |
Now these are very simple but of ‘dubious’ SEO value or as Jeff put it:
…any post titles that are longer than one word will break the validity of the web page….[without] the urlencode() functionality,
So I would suggest having a quick look at his article on social media links also.
Now that is the main ‘crux’ of the code other options that I am considering are:
> adding images for the social media links,
> optimise the code for speed!
> code in a backend so you can easily add your own custom messages for custom domains.
aaaand that’s all folk!
Have fun playing with the code, I will try and answer any questions and if you get it working throw me a link so I can check it out.
Popularity: 29% [?]
Related posts:
- Beware the smiling man A few weeks ago I stumbled across some interesting spam; it was generic enough to make me think hmm is this spam or a...
Incredible article, Donace! I snagged an offline copy to sneak into work with me this weekend; looking forward to savoring the details!
Sweet man, have fun and if you can think of any improvements/ amendments feel free to tell me :p
This is cool. I haven’t felt the need to greet my visitors because I’m “anti-social” but I like it. And now I see that you also know Jeff … don’t know why I didn’t realize that before.
lol! Thanks for dropping by Kim, the main aim of this was to enhance social media voting, and I hope this will help the occasional user to click and help out…if they think the post rocked!
Yes I know Jeff that is actually how I stumbled across your site for the first time.
Donace, the main reason to use , and the main reason Thaya went with JS and not PHP version is that it supports caching out of the box. WP-Supercache is a very popular plugin and if you use a PHP version, it’ll cache the greeting and display it to other people.
Thanks for dropping by Artem.
Indeed that is a valid consideration, but on the flip side; a lot of users use ‘noscript’ addon for Firefox etc and is ‘heavier’ and ’slower’ in my opinion.
Also in regards to the caching issue as far as I recall all it will require is a small line at the beginning and end of the code to stop it being cached…thus getting the speed benefits (and versatility) of php and still getting the caching benefits elsewhere.
Further more if done ‘correctly’ you could setup an internal cache for each referrer at times where you get major digg traffic etc.
Though as I mentioned in this article I haven’t had time to perfect the code as viable plugin or otherwise. Though when I have time I shall attempt to illustrate what I mean.
Donace, Superb!
BTW where do you put this whole code in your wordpress theme so that it is shown to the visitors?
anywhere you want man!