开发者

PHP function to check if browser referer is empty?

I was using this function

ereg('http://.',$_SERVER['HTTP_REFERER']) != 1

To check if the visitor clicked a link or did enter directly to this particular page.

I see now that ereg is deprec开发者_如何学Pythonated, but have no knowledge of php to convert this to maybe preg_match?

Is there any other way to check if the referer is empty?

Thanks a lot!!


Your regex accepts only referers that start with http://. So if somebody would navigate to your script from a https:// page, the referer would register as empty. Any specific reason for that?

If not, using empty() will always work:

if (!empty($_SERVER['HTTP_REFERER']))


empty($_SERVER['HTTP_REFERER'])


For empty:

!empty($_SERVER['HTTP_REFERER']);

For a protocol:

strlen(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_SCHEME)) > 0


Try this:

if (preg_match('#http://#', $_SERVER['HTTP_REFERER'])){
  // match found...
}

Or if you want to check whether or not it is empty, use the empty function:

if (!empty($_SERVER['HTTP_REFERER'])){
  // match found...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜