The quick version: I’ve found a way to abuse the login mechanism for both Twitter and Google to detect whether a user is logged in to that service. Facebook provides an API for this. So I provide a cross-browser javascript template that works for all 3 networks. If you want to get straight to the code jump to the implementation section or check out the Social Network Login Status Detector Demo.
Introduction
I was interested in seeing whether it would be possible to track which social networks a website visitor is logged into at the time of their visit; it could be cool for selecting which social media buttons you show them, what sort of marketing you do to them, or simply to evaluate whether you should be participating more on a certain social network. I was interested in Facebook, Twitter and Google+; as an SEO I was also interested in whether people were logged into a general Google account so I could compare which percentage of those had a Google+ account.
A quick search turned up an interesting post from Mike Cardwell who had a method for doing this for Facebook, Twitter and Gmail, but unfortunately it didn’t work in Internet Explorer. Secondly, I knew there was a better method than Mike’s for Facebook, which I’d seen presented by Mat Clayton of Mixcloud; he uses Facebook’s API to do the same thing (see slide 15). Mat’s method works great across browsers, so that solved the Facebook side of this.
Finding a way in to Twitter and Google+
Wat I needed was a method for detecting whether a visitor to my site was logged in to Twitter, Google and more specifically Google+.
Thanks to abraham from Hacker News I discovered that Twitter has an undocumented endpoint that simply returns true or false for whether the current user is logged in! It is very simple:
-
<script>
-
function twitterLoginStatus(state) {
-
alert(state);
-
}
-
</script>
-
<script src='https://api.twitter.com/sessions/present.js?callback=twitterLoginStatus'></script>
However, due to boring technical details concerning MIME types this code doesn’t work on IE9, which (unfortunately) for many purposes makes it less than ideal.
Browsers nowadays are very sensitive to cross site requests and the all to common exploits that abuse them, and so unless the 3rd party site plans to allow it using javascript for this is probably going to be difficult. The other great way to make cross domain requests is with image tags.
Tricking login mechanisms
I came up with the theory that I needed to try to access and image on Twitter/Google’s sites that would only be available to users when they are logged in. Using javascript I could detect whether the image loaded or not and thus determine whether the user was loggedin. However, these are obviously going to be few and far between (image assets are often static and so on CDNs and/or not protected in such a manner), if they exist at all (I didn’t find any), so I was back to square one. I needed a protected area of the site, but needed the file contents to be an image.
My winning moment was realising that some naive login systems might be open to abuse for exactly this purpose. It is often the case that you try to access a specific page on a site, lets say the “Upload a photo” page but you need to be logged in to do so. If you are not logged into the site in question, when you visit the URL the page redirects to the Login page to authenticate you are who you say you are; however the site wants to be helpful and send you to the page you were looking for so they keep a track of that target page in the URL as a parameter and then helpfully redirect you to that page after login is complete.
What happens if you visit the login page with a ‘redirect on login’ parameter and you are already logged in? When implemented in a naive fashion you are simply immediately redirected to the page specified in the parameter. Some sites limit that parameter to being another page on the same domain, but we’ll see that doesn’t help for this trick.
This mechanism is open to abuse in exactly the way I needed; I could set the ‘redirect on login’ page to be an image file on the same domain. For example:
-
<img src="https://twitter.com/login?redirect_after_login=%2Fimages%2Fspinner.gif" />
In this example, if I am logged in Twitter is kind enough to 302 redirect me to the image file I specified, but if I am not logged in I am show the login page. It turns out that both Twitter and Google’s login mechanisms are susceptible to exactly this trick. It seems LinkedIn and Tumblr are currently immune to this, though I didn’t dig too deep so there might be another redirect URL for them.
Putting it all together
From this point on it was quite easy to hack together some javascript; just stick this code in the <head></head> section of your page:
-
<script type="text/javascript">
-
function show_login_status(network, status)
-
{
-
if (status)
-
{
-
alert("Logged in to " + network);
-
}else{
-
alert("Not logged in to " + network);
-
}
-
}
-
</script>
Then, anywhere in your code that seems like a nice place stick this HTML:
-
<img style="display:none;"
-
onload="show_login_status('Google', true)"
-
onerror="show_login_status('Google', false)"
-
src="https://accounts.google.com/CheckCookie?continue=https%3A%2F%2Fwww.google.com%2Fintl%2Fen%2Fimages%2Flogos%2Faccounts_logo.png&followup=https%3A%2F%2Fwww.google.com%2Fintl%2Fen%2Fimages%2Flogos%2Faccounts_logo.png&chtml=LoginDoneHtml&checkedDomains=youtube&checkConnection=youtube%3A291%3A1"
-
/>
-
-
<img style="display:none;"
-
onload="show_login_status('GooglePlus', true)"
-
onerror="show_login_status('GooglePlus', false)"
-
src="https://plus.google.com/up/?continue=https://www.google.com/intl/en/images/logos/accounts_logo.png&type=st&gpsrc=ogpy0"
-
/>
-
-
<img style="display:none;" src="https://twitter.com/login?redirect_after_login=%2Fimages%2Fspinner.gif" onload="show_login_status('Twitter', true)" onerror="show_login_status('Twitter', false)" />
-
-
<div id="fb-root"></div>
Finally, somewhere after that HTML stick this Javascript:
-
<script>
-
window.fbAsyncInit = function(){
-
FB.init({ appId:'xxxxxxxxxxxx', status:true, cookie:true, xfbml:true});
-
FB.getLoginStatus(function(response){
-
if (response.status != "unknown")
-
{
-
show_login_status("Facebook", true);
-
}else{
-
show_login_status("Facebook", false);
-
}
-
});
-
};
-
// Load the SDK Asynchronously
-
(function(d){
-
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
-
js = d.createElement('script'); js.id = id; js.async = true;
-
js.src = "//connect.facebook.net/en_US/all.js";
-
d.getElementsByTagName('head')[0].appendChild(js);
-
}(document));
-
</script>
You will need to replace xxxxxxxxx above with the appID for an app created for your domain; if you don’t have one you can create one in about 60 seconds. Simply visit https://developers.facebook.com/apps whilst logged in to Facebook, and click “Create New App”. You will be prompted for a “Display Name”, and you can enter any old dummy text here and press Continue. On the next page it is only necessary to fill out the “App Domain” and the “Website” with the URL of the domain you want to use this code on. Do that, save changes and grab the “App ID” from the top of the page and enter it in the code above.
You should be all set! Now you can change your alert() functions to do whatever you want based on the login status of the user. See a demo of it in action Social Network Login Status Detector Demo.
Wrap up
In my testing this worked on a range of versions of Firefox and Chrome, IE versions 7 and up, Safari and Opera. It may be that these loopholes get fixed, but in the meantime I implore you to only use this in nice ways. There is an argument that a 3rd party even knowing what other sites you are logged into is a breach of your privacy, and I can certainly see why some people would feel like that (especially if this was scaled up to more personal sites that you might be logged into). If you want to prevent this then for Firefox you can try RequestPolicy or NoScript. For Chrome you can give ScriptNo a shot. On IE you can try giving Firefox or Chrome a try.
However, I do also think that this sort of thing can be used in good ways – serving only a subset of social buttons to your users, or determining whether you should be providing support on a given social platform etc. If anyone has any nice suggestions for other ways you could use (nicely) use this, I’d love to hear.


Check if User is Logged Into ANY Site - Facebook, Google Plus, Gmail, StumbleUpon, etc
March 28, 2013 at 8:50 pm[...] can find more details and elaborations here: https://grepular.com/Abusing_HTTP_St…te_Information http://www.tomanthony.co.uk/blog/det…cial-networks/ I must say though that finding whether a user is logged in or not is very useful in Clickjacking [...]
株式会社ISSUN(イッスン) SEO的に見た、ECサイトで「あなたが取り組むべき15のページ要素」2012年版 » 株式会社ISSUN(イッスン)
April 13, 2013 at 5:11 pm[...] また、facebookやtwitterなどにログインしているユーザに、特別なメッセージを表示させることでシェアを誘発する方法も可能で、これは興味深い解説でした。http://www.tomanthony.co.uk/blog/detect-visitor-social-networks/ [...]
Updated Code: Which Social Networks Are Your Visitors Logged Into | Alex Czartoryski's Blog
June 1, 2013 at 2:13 am[...] in February, Tom Anthony wrote a couple of great posts on how to detect if visitors were logged into a social network and then how to use Google Analytics to track this [...]
Susan
June 17, 2013 at 4:20 amThis was just the bit of code I was looking for to fix a google docs glitch. Thanks!
http://www.joshualawrencechamberlain.com/autobiography.php
mohamed
June 25, 2013 at 7:23 pmhhhhhhhhhhhhhh hiiiiiiiii
e-ticaret
July 25, 2013 at 12:09 pmThank you so much.
3 Cool Analytics Hacks in Google's new Universal Analytics.js
August 3, 2013 at 7:36 am[...] final tip actually comes courtesy of Tom Anthony who first wrote about this last year on his personal blog here. However, as with other things in this article the actual implementation needs to change in order [...]
Twitjack 100,000 followers! - Web Marketing School
September 11, 2013 at 5:31 pm[...] 2) identify which visitors are logged into twitter/G+/facebook etc. – you can do this using a technique written about in much better detail that I will go into here by a friend of mine and allround top SEO bloke, Tom Anthony [...]
Alex
September 21, 2013 at 9:51 amVery interesting approach to this challenge. I have been trying to think of a way to detect whether someone is logged into a social network or other service, but wasn’t able to figure it out. Looks like you’ve solved this, at least temporarily, until they change something so that this no longer works.
Alan
September 28, 2013 at 7:05 pmI try to detect visitators from social netkworm from year but useles! Now after I read this article I will try to follow your advices and I hope to work!
Thanks for this great article!
Gil Peron
October 15, 2013 at 6:47 pmReally nice. I setted it up and worked fine. It still works fine in Twitter em G+. With Facebook I have an error message saying the SDK version is not supported anymore! Any idea how to fix? Anyway, thank you so much!
How to get almost every visitor to like your website (Facebook) | Aktarer Zaman
October 17, 2013 at 6:51 am[...] is set up is that it first detects if the visitor is logged into facebook. It does this through a hack revealed here. If you are not logged in, it removes the trailing iframe. If it doesn’t do this, a Facebook [...]
LOL
December 4, 2013 at 12:53 pmyour twitter demo doesn’t work for me :/
Isaiah Joe
December 10, 2013 at 1:54 amTom, you really over did it here, thanks for this update, going to add this in my meme website. thanks once more.
chuang
December 11, 2013 at 4:36 pmI am a beginner at this, but is it possible to indentify the accounts who are logged in? it is possible to know if somone is logged in in a social media site, but is it possible to know the name/adress of that account? If that is possible than clearing cookies is useless because they will track you anyway everytime you are logged in.
Jay
December 20, 2013 at 12:23 amvery cool!
is there a way to detect Pinterest login status?
eric
January 14, 2014 at 7:48 pmLove it!
Is it possible to detect their username or avatar?
Priayl Gor
March 21, 2014 at 1:07 pmThis is smart but rather bordering on the privacy issues.
Anyone got any ideas to put it to good use ?
Greg
March 25, 2014 at 5:00 pmyour twitter demo doesn’t work anymore for me either :/
Stacey
March 26, 2014 at 12:55 pmHi Tom
The Twitter demo doesn’t seem to be working anymore. Do you have any ideas on how to fix this?
Roman
March 27, 2014 at 1:46 pmHi Tom,
is there a way to check whether the user is logged in a social network and if yes, show their profile picture and name somewehere on the website?
Thanks, Roman
William Entriken
April 21, 2014 at 3:20 pmThe proof of concept failed to recognize that I was currently logged in to Twitter.
5 tähden maine
May 12, 2014 at 1:31 pmNice solution to a dilemma we have had for some time now. It’s all nice to send your user to a G+ service but it get’s very nice when you can add logic according to their login/off status. Thanks!
Jason
May 16, 2014 at 4:30 pmvery good
William Entriken
May 19, 2014 at 2:18 pmHello, would you like to post this on GitHub, I would like to help maintain and add some improvements.
gamble online with bonuses
June 10, 2014 at 11:13 pmThis is something i can use to improve my social networking for people wanting to find thelatest and best bonuses for gambling
ebooks
June 19, 2014 at 3:22 amI would like to help maintain in github
ayus
July 16, 2014 at 7:08 pmNice post. Is there anyway we can get the username for each as well. For e.g. if a user is logged on to gmail, fb or twitter then display the associated username or email related to the account?
Bernardo Alba
July 30, 2014 at 12:09 pmYou and me both
Checking if a user has authenticated my app in Twitter
July 29, 2014 at 8:40 am[…] I have an app that allows facebook and twitter logins. For facebook, there’s a javascript API that can tell me, on page load, if a user is both logged into facebook and connected to my app. This is useful in case their session has expired, so I can easily log them in again. However, with twitter, I haven’t been able to find anything like this. There’s a very hacky way of detecting if the user has logged in (taken from here): […]
penny
August 18, 2014 at 3:01 pmCompanyspot Camberley Camberley – search in UK companies and organisations
Henry Valdez Polanco
August 21, 2014 at 6:55 pmExcelente
Too powerfull in evil hands xD
Loginstatus für Twitter, Facebook oder Google+ bestimmen. | No more cubes.
August 27, 2014 at 9:55 am[…] via Detect if visitors are logged into Twitter, Facebook or Google+. […]
Detect if visitors are logged into Twitter, Facebook or Google+ - Jacob is studying on web programming
October 13, 2014 at 2:15 am[…] http://www.tomanthony.co.uk/blog/detect-visitor-social-networks/ 에서 참조 […]
ux arab
November 9, 2014 at 10:59 amhow can detect name and family
please help me i need that code
Iulia
November 14, 2014 at 11:03 amI have an app and I am interested if I can get a list of places that my friends checked in .For example in my birth city, I want to take all the places, and how many users checked in into each place. I would appreciate if you have any idea or suggestion.
jualjamtangankwsupermurah
November 14, 2014 at 2:39 pmHello, would you like to post this on GitHub, I would like to help maintain and add some improvements.http://jualjamtangankwsupermurah.com/
jam tangan original
jam tangan online
November 17, 2014 at 4:13 amThanks for your guideline.
Detect if visitors are logged into Twitter, Facebook or Google+ - Jacob is studying on web programming
December 19, 2014 at 12:49 am[…] javascript template that works for all 3 networks. If you want to get straight to the code jump to the implementation section or check out the Social Network Login Status Detector […]
jayenne
January 19, 2015 at 12:17 pmHi, Does the twitter method still work? I can’t get anything from it ATM and wondered if twitter have broken this method?
Determining whether users are logged into Twitter | Reflections
February 27, 2015 at 9:06 pm[…] Detect if visitors are logged into Twitter, Facebook or Google+, Tom Anthony explains how to determine what social networks your users are logged into. The […]
John Karahalis
February 27, 2015 at 9:31 pmThanks for sharing this. This was a big help in some social media research we conducted on MDN. I wrote a follow-up blog post with an updated Twitter example for anyone interested.
jhunax
March 5, 2015 at 1:46 pmHey it’s a nice post you got here and I enjoyed reading your article.I hope to find some nice news here. And of course I bookmarked it, Thank you for sharing!! Penis vergrössern mit Eigenfett
prashant
March 28, 2015 at 6:51 amSir,May i know the email id of visitor who is currently logged in.
fin zap
April 8, 2015 at 3:16 pmNice site! Do you know some linked in things too?