Hello Visitor- The Quest to Greet

Posted on December 10th, 2008 by Donace in Coding

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&amp;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&amp;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&amp;story=PERMALINK',
 
'blinkbits' 
'url' => 'http://www.blinkbits.com/bookmarklets/save.php?v=1&amp;source_url=PERMALINK&amp;title=TITLE&amp;body=TITLE',
 
'BlinkList' 
'url' => 'http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=PERMALINK&amp;Title=TITLE',
 
'BlogMemes' 
'url' => 'http://www.blogmemes.net/post.php?url=PERMALINK&amp;title=TITLE',
 
'BlogMemes Fr
'url' => 'http://www.blogmemes.fr/post.php?url=PERMALINK&amp;title=TITLE',
 
'BlogMemes Sp' 
'url' => 'http://www.blogmemes.com/post.php?url=PERMALINK&amp;title=TITLE',
 
'BlogMemes Cn' 
'url' => 'http://www.blogmemes.cn/post.php?url=PERMALINK&amp;title=TITLE',
 
'BlogMemes Jp'
'url' => 'http://www.blogmemes.jp/post.php?url=PERMALINK&amp;title=TITLE',
 
'blogmarks' 
'url' => 'http://blogmarks.net/my/new.php?mini=1&amp;simple=1&amp;url=PERMALINK&amp;title=TITLE',
 
'Blogosphere News' 
'url' => 'http://www.blogospherenews.com/submit.php?url=PERMALINK&amp;title=TITLE',
 
'Blogsvine' 
'url' => 'http://blogsvine.com/submit.php?url=PERMALINK',
 
'blogtercimlap'
'url' => 'http://cimlap.blogter.hu/index.php?action=suggest_link&amp;title=TITLE&amp;url=PERMALINK',
 
'Blue Dot' 
'url' => 'http://bluedot.us/Authoring.aspx?u=PERMALINK&amp;title=TITLE',
 
'Book.mark.hu' 
'url' => 'http://book.mark.hu/bookmarks.php/?action=add&amp;address=PERMALINK%2F&amp;title=TITLE',
 
'Bumpzee' 
'url' => 'http://www.bumpzee.com/bump.php?u=PERMALINK',
 
'co.mments' 
'url' => 'http://co.mments.com/track?url=PERMALINK&amp;title=TITLE',
 
'connotea' 
'url' => 'http://www.connotea.org/addpopup?continue=confirm&amp;uri=PERMALINK&amp;title=TITLE',
 
'del.icio.us'
'url' => 'http://del.icio.us/post?url=PERMALINK&amp;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&amp;title=TITLE',
 
'Digg' 
'url' => 'http://digg.com/submit?phase=2&amp;url=PERMALINK&amp;title=TITLE',
 
'DotNetKicks' 
'url' => 'http://www.dotnetkicks.com/kick/?url=PERMALINK&amp;title=TITLE',
 
'DZone' 
'url' => 'http://www.dzone.com/links/add.html?url=PERMALINK&amp;title=TITLE',
 
'eKudos' 
'url' => 'http://www.ekudos.nl/artikel/nieuw?url=PERMALINK&amp;title=TITLE',
 
'Facebook' 
'url' => 'http://www.facebook.com/sharer.php?u=PERMALINK&amp;t=TITLE',
 
'Fark' 
'url' => 'http://cgi.fark.com/cgi/fark/edit.pl?new_url=PERMALINK&amp;new_comment=TITLE&amp;new_comment=BLOGNAME&amp;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&amp;op=submit&amp;url=PERMALINK&amp;name=TITLE',
 
'Furl' 
'url' => 'http://www.furl.net/storeIt.jsp?u=PERMALINK&amp;t=TITLE',
 
'Fleck' 
'url' => 'http://extension.fleck.com/?v=b.0.804&amp;url=PERMALINK',
 
'Global Grind' 
'url' => 'http://globalgrind.com/submission/submit.aspx?url=PERMALINK&amp;type=Article&amp;title=TITLE'
 
'Google' 
'url' => 'http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=PERMALINK&amp;title=TITLE'
 
'Gwar' 
'url' => 'http://www.gwar.pl/DodajGwar.html?u=PERMALINK',
 
'Haohao' 
'url' => 'http://www.haohaoreport.com/submit.php?url=PERMALINK&amp;title=TITLE',
 
'HealthRanker' 
'url' => 'http://healthranker.com/submit.php?url=PERMALINK&amp;title=TITLE',
 
'Hemidemi' 
'url' => 'http://www.hemidemi.com/user_bookmark/new?title=TITLE&amp;url=PERMALINK',
 
'IndiaGram' 
'url' => 'http://www.indiagram.com/mock/bookmarks/desitrain?action=add&amp;address=PERMALINK&amp;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&amp;title=TITLE',
 
'Kirtsy' 
'url' => 'http://www.kirtsy.com/submit.php?url=PERMALINK&amp;title=TITLE',
 
'laaik.it'
'url' => 'http://laaik.it/NewStoryCompact.aspx?uri=PERMALINK&amp;headline=TITLE&mp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12',
 
'LinkArena' 
'url' => 'http://linkarena.com/bookmarks/addlink/?url=PERMALINK&amp;title=TITLE',
 
'LinkaGoGo' 
'url' => 'http://www.linkagogo.com/go/AddNoPopup?url=PERMALINK&amp;title=TITLE',
 
'LinkedIn' 
'url' => 'http://www.linkedin.com/shareArticle?mini=true&amp;url=PERMALINK&amp;title=TITLE&amp;source=BLOGNAME&amp;summary=EXCERPT',
 
'Linkter' 
'url' => 'http://www.linkter.hu/index.php?action=suggest_link&amp;url=PERMALINK&amp;title=TITLE',
 
'Live' 
'url' => 'https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=PERMALINK&amp;title=TITLE',
 
'Ma.gnolia' 
'url' => 'http://ma.gnolia.com/bookmarklet/add?url=PERMALINK&amp;title=TITLE',
 
'Meneame' 
'url' => 'http://meneame.net/submit.php?url=PERMALINK',
 
'MisterWong' 
'url' => 'http://www.mister-wong.com/addurl/?bm_url=PERMALINK&amp;bm_description=TITLE&amp;plugin=soc',
 
'MisterWong.DE' 
'url' => 'http://www.mister-wong.de/addurl/?bm_url=PERMALINK&amp;bm_description=TITLE&amp;plugin=soc',
 
'Mixx' 
'url' => 'http://www.mixx.com/submit?page_url=PERMALINK&amp;title=TITLE',
 
'muti' 
'url' => 'http://www.muti.co.za/submit?url=PERMALINK&amp;title=TITLE',
 
 
'MyShare' 
'url' => 'http://myshare.url.com.tw/index.php?func=newurl&amp;url=PERMALINK&amp;desc=TITLE',
 
'N4G' 
'url' => 'http://www.n4g.com/tips.aspx?url=PERMALINK&amp;title=TITLE',
 
'NewsVine' 
'url' => 'http://www.newsvine.com/_tools/seed&amp;save?u=PERMALINK&amp;h=TITLE',
 
'Netvouz' 
'url' => 'http://www.netvouz.com/action/submitBookmark?url=PERMALINK&amp;title=TITLE&amp;popup=no',
 
'NuJIJ' 
'url' => 'http://nujij.nl/jij.lynkx?t=TITLE&amp;u=PERMALINK',
 
'PlugIM' 
'url' => 'http://www.plugim.com/submit?url=PERMALINK&amp;title=TITLE',
 
'PopCurrent' 
'url' => 'http://popcurrent.com/submit?url=PERMALINK&amp;title=TITLE&amp;rss=RSS',
 
'Pownce' 
'url' => 'http://pownce.com/send/link/?url=PERMALINK&amp;note_body=TITLE&amp;note_to=all'
 
'ppnow' 
'url' => 'http://www.ppnow.net/submit.php?url=PERMALINK',
 
'Propeller'
'url' => 'http://www.propeller.com/submit/?U=PERMALINK&amp;T=TITLE',
 
'Ratimarks'
'url' => 'http://ratimarks.org/bookmarks.php/?action=add&address=PERMALINK&amp;title=TITLE',
 
'RawSugar'
'url' => 'http://www.rawsugar.com/tagger/?turl=PERMALINK&amp;tttl=TITLE',
 
'Rec6' 
'url' => 'http://www.syxt.com.br/rec6/link.php?url=PERMALINK&amp;=TITLE',
 
'Reddit'
'url' => 'http://reddit.com/submit?url=PERMALINK&amp;title=TITLE',
 
'SalesMarks' 
url' => 'http://salesmarks.com/submit?edit[url]=PERMALINK&amp;edit[title]=TITLE',
 
'Scoopeo' 
'url' => 'http://www.scoopeo.com/scoop/new?newurl=PERMALINK&amp;title=TITLE',
 
'scuttle' 
'url' => 'http://www.scuttle.org/bookmarks.php/maxpower?action=add&amp;address=PERMALINK&amp;title=TITLE',
 
'Segnalo' 
'url' => 'http://segnalo.alice.it/post.html.php?url=PERMALINK&amp;title=TITLE',
 
'Shadows'
'url' => 'http://www.shadows.com/features/tcr.htm?url=PERMALINK&amp;title=TITLE',
 
'Simpy'
'url' => 'http://www.simpy.com/simpy/LinkAdd.do?href=PERMALINK&amp;title=TITLE',
 
'Slashdot' 
'url' => 'http://slashdot.org/bookmark.pl?title=TITLE&amp;url=PERMALINK',
 
'Smarking' 
'url' => 'http://smarking.com/editbookmark/?url=PERMALINK&amp;title=TITLE',
 
'Socialogs' 
'url' => 'http://socialogs.com/add_story.php?story_url=PERMALINK&amp;story_title=TITLE',
 
'Spurl' 
'url' => 'http://www.spurl.net/spurl.php?url=PERMALINK&amp;title=TITLE',
 
'SphereIt' 
'url' => 'http://www.sphere.com/search?q=sphereit:PERMALINK&amp;title=TITLE',
 
'Sphinn' 
'url' => 'http://sphinn.com/submit.php?url=PERMALINK&amp;title=TITLE',
 
'Taggly' 
'url' => 'http://taggly.com/bookmarks.php/pass?action=add&amp;address=',
 
'Technorati' 
'url' => 'http://technorati.com/faves?add=PERMALINK',
 
'TailRank'
'url' => 'http://tailrank.com/share/?text=&amp;link_href=PERMALINK&amp;title=TITLE',
 
'ThisNext' 
'url' => 'http://www.thisnext.com/pick/new/submit/sociable/?url=PERMALINK&amp;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&amp;title=TITLE',
 
'Webride' 
'url' => 'http://webride.org/discuss/split.php?uri=PERMALINK&amp;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=&amp;r=PERMALINK&amp;title=TITLE',
 
'Wykop' 
'url' => 'http://www.wykop.pl/dodaj?url=PERMALINK',
 
'Xerpi' 
'url' => 'http://www.xerpi.com/block/add_link_from_extension?url=PERMALINK&amp;title=TITLE',
 
'YahooMyWeb' 
'url' => 'http://myweb2.search.yahoo.com/myresults/bookmarklet?u=PERMALINK&amp;=TITLE',
 
'Yigg' 
'url' => 'http://yigg.de/neu?exturl=PERMALINK&amp;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:

  1. 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...

8 Comments

  • At 2008.12.10 11:28, Jeff Starr said:

    Incredible article, Donace! I snagged an offline copy to sneak into work with me this weekend; looking forward to savoring the details! :)

    • At 2008.12.10 12:25, Donace said:

      Sweet man, have fun and if you can think of any improvements/ amendments feel free to tell me :p

    • At 2008.12.17 17:33, Kim Woodbridge said:

      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.

      • At 2008.12.17 17:55, Donace said:

        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.

      • At 2008.12.28 12:14, Artem Russakovskii said:

        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.

        • At 2008.12.28 12:27, Donace said:

          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.

        • At 2009.02.19 18:53, Nihar said:

          Donace, Superb!

          BTW where do you put this whole code in your wordpress theme so that it is shown to the visitors?

          • At 2009.04.14 02:11, Donace said:

            anywhere you want man!

          (A must)
          (Another Must but dont worry will not be published)

          Archives

          Full Archive

          Tag Cloud

          .htaccess adgitize Alexa Internet automation Backlink Backlinks Blog bot Bots code competitons Contest copyright entrecard Firefox Google Google Page Rank How to howto Law link building Link Love links news Optimization PageRank PHP plugin Programming Promotion Rants of a loony toon rapidshare Search Engines Security SEO Site update Site updates Spammers TheDuke traffic tutorial updates Weblogs Webmaster Web traffic