How do I get the referring site a user came from to arive on my site using PHP?

强颜欢笑 提交于 2019-12-10 19:17:15

问题


I am trying to create the ability to have promo codes on the site that I am working on. Part of this functionality requires me to get the referring site the user came from to give them the appropriate promo code..

I was thinking of using

$_SERVER['HTTP_REFERER']

to get the referring site but reading this

'HTTP_REFERER'

The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.

It seems like not the best approach or is it. Is there a better way to get the site the user came from and landed on my site.


回答1:


If you can't control the page the user is coming from, I'm afraid HTTP_REFERER is the only, and thus the best approach to find out a referring page.

There are none that are more trustworthy. You could use a referrer code in the GET string, but that could be manipulated or even inadvertently copied just as easily.




回答2:


If the referrer is a page on your own server, you could use a session to determine the last page the user visited.

If you are looking for external referrals, $_SERVER['HTTP_REFERRER'] is your only course of action.




回答3:


If you want to be able to rely on what site they came from, and you're dealing with a known set of possible referrers, you might arrange with the various possible sites for them to add a code to the URL that indicates where the link came from. This requires, of course, that the sites in question are more like partners; Google isn't going to make a deal with you.

HTTP_REFERER is relatively easy to spoof; if the site you're working on is prominent enough, or the promo codes are valuable enough, people may well "steal" the promo codes by spoofing the referrer (if your rely on HTTP_REFERER).



来源:https://stackoverflow.com/questions/7151647/how-do-i-get-the-referring-site-a-user-came-from-to-arive-on-my-site-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!