开发者

How to use strstr with $_SERVER['HTTP_REFERER']

开发者 https://www.devze.com 2022-12-21 09:48 出处:网络
I\'m using php to make it so that incoming traffic to my website will append information depending on the search engine.I\'m setting $_SESSION[\'kw\'], if it\'s not already set, to tel me that it\'s s

I'm using php to make it so that incoming traffic to my website will append information depending on the search engine. I'm setting $_SESSION['kw'], if it's not already set, to tel me that it's seo and to append the search terms:

  if (isset($result['q']))
   $_SESSION['kw'] = 'seo-'.str_replace('\\"',"X",$result['q']);

  elseif (isset($result['p']))
   $_SESSION['kw'] = 'seo-'.$result['p'];

else {some other stuff}

q is what google, msn and ask use for their queries, p is for yahoo. That covers most of the incoming traffic. OK so now I want to change it a bit so that if the referring page is yahoo, it appends a Y, if it's google a G, msn M etc.

I tried:

if (isset($result['q'])& strstr($_SERVER['HTTP_REFERER'],'google'))
   $_SESSION['kw'] = 'seo-G-'.str_replace('\\"',"X",$result['q']);

elseif (isset($result['q'])& !strstr($_SERVER['HTTP_REFERER'],'google'))
   $_SESSION['kw'] = 'seo-M-'.str_replace('\\"',"X",$result['q']);

  elseif (isset($result['p']))开发者_如何转开发
   $_SESSION['kw'] = 'seo-Y-'.$result['p'];

    else {some other stuff}

But I can't make the first IF true. If I come from ask.com, I get "seo-M appended", but I can't even when coming from google, get it to append. Am I just formatting it wrong?


Change the & to &&. This is the usual operator for most programming languages for a logical "and"

In PHP you have && or AND for logical bool operations.

& is usually used for bit-operations, which is wrong here.


I think you're using the wrong AND operator, you should use && not just &. the single ampersand is for bitwise operations.

Try:

if (isset($result['q']) && strstr($_SERVER['HTTP_REFERER'],'google'))
0

精彩评论

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