Since the http referer ($_SERVER['HTTP_REFERER']
) won't be set consistently and there will be the support of rel=noreferrer
attributes on anchors soon, this could be tricky.
But since you are the one to share the links on social media, you could add a get parameter like:
myFancyDomain.com?referrer=facebook
This is the only consistent way I could imagine :)
I guess this is a good quote (even it's kinda old):
The "Referer" header entry is optional. You cannot rely on it being
present. There is no cross-browser way to get the previous link
because this depends on the user settings and proxy configuration
(i.e. what the system administrators think they should allow you to
see).
You must find a way to live without this information.
Source